Authentication
Learn how to implement authentication in your Next.js application.
Understanding authentication is crucial for protecting your application's data. This page will guide you through what React and Next.js features to use to implement auth.
Before starting, it helps to break down the process into three concepts:
- Authentication: Verifies if the user is who they say they are. It requires the user to prove their identity with something they have, such as a username and password.
- Session Management: Tracks the user's auth state across requests.
- Authorization: Decides what routes and data the user can access.
This diagram shows the authentication flow using React and Next.js features:
The examples on this page walk through basic username and password auth for educational purposes. While you can implement a custom auth solution, for increased security and simplicity, we recommend using an authentication library. These offer built-in solutions for authentication, session management, and authorization, as well as additional features such as social logins, multi-factor authentication, and role-based access control. You can find a list in the Auth Libraries section.
Authentication
Sign-up and login functionality
You can use the <form>
element with React's Server Actions and useActionState
to capture user credentials, validate form fields, and call your Authentication Provider's API or database.
Since Server Actions always execute on the server, they provide a secure environment for handling authentication logic.
Here are the steps to implement signup/login functionality:
1. Capture user credentials
To capture user credentials, create a form that invokes a Server Action on submission. For example, a signup form that accepts the user's name, email, and password:
2. Validate form fields on the server
Use the Server Action to validate the form fields on the server. If your authentication provider doesn't provide form validation, you can use a schema validation library like Zod or Yup.
Using Zod as an example, you can define a form schema with appropriate error messages:
To prevent unnecessary calls to your authentication provider's API or database, you can return
early in the Server Action if any form fields do not match the defined schema.
Back in your <SignupForm />
, you can use React's useActionState
hook to display validation errors while the form is submitting:
Good to know:
- In React 19,
useFormStatus
includes additional keys on the returned object, like data, method, and action. If you are not using React 19, only thepending
key is available. - Before mutating data, you should always ensure a user is also authorized to perform the action. See Authentication and Authorization.
3. Create a user or check user credentials
After validating form fields, you can create a new user account or check if the user exists by calling your authentication provider's API or database.
Continuing from the previous example:
After successfully creating the user account or verifying the user credentials, you can create a session to manage the user's auth state. Depending on your session management strategy, the session can be stored in a cookie or database, or both. Continue to the Session Management section to learn more.
Tips:
- The example above is verbose since it breaks down the authentication steps for the purpose of education. This highlights that implementing your own secure solution can quickly become complex. Consider using an Auth Library to simplify the process.
- To improve the user experience, you may want to check for duplicate emails or usernames earlier in the registration flow. For example, as the user types in a username or the input field loses focus. This can help prevent unnecessary form submissions and provide immediate feedback to the user. You can debounce requests with libraries such as use-debounce to manage the frequency of these checks.
Session Management
Session management ensures that the user's authenticated state is preserved across requests. It involves creating, storing, refreshing, and deleting sessions or tokens.
There are two types of sessions:
- Stateless: Session data (or a token) is stored in the browser's cookies. The cookie is sent with each request, allowing the session to be verified on the server. This method is simpler, but can be less secure if not implemented correctly.
- Database: Session data is stored in a database, with the user's browser only receiving the encrypted session ID. This method is more secure, but can be complex and use more server resources.
Good to know: While you can use either method, or both, we recommend using a session management library such as iron-session or Jose.
Stateless Sessions
To create and manage stateless sessions, there are a few steps you need to follow:
- Generate a secret key, which will be used to sign your session, and store it as an environment variable.
- Write logic to encrypt/decrypt session data using a session management library.
- Manage cookies using the Next.js
cookies
API.
In addition to the above, consider adding functionality to update (or refresh) the session when the user returns to the application, and delete the session when the user logs out.
Good to know: Check if your auth library includes session management.
1. Generating a secret key
There are a few ways you can generate secret key to sign your session. For example, you may choose to use the openssl
command in your terminal:
This command generates a 32-character random string that you can use as your secret key and store in your environment variables file:
You can then reference this key in your session management logic:
2. Encrypting and decrypting sessions
Next, you can use your preferred session management library to encrypt and decrypt sessions. Continuing from the previous example, we'll use Jose (compatible with the Edge Runtime) and React's server-only
package to ensure that your session management logic is only executed on the server.
Tips:
- The payload should contain the minimum, unique user data that'll be used in subsequent requests, such as the user's ID, role, etc. It should not contain personally identifiable information like phone number, email address, credit card information, etc, or sensitive data like passwords.
3. Setting cookies (recommended options)
To store the session in a cookie, use the Next.js cookies
API. The cookie should be set on the server, and include the recommended options:
- HttpOnly: Prevents client-side JavaScript from accessing the cookie.
- Secure: Use https to send the cookie.
- SameSite: Specify whether the cookie can be sent with cross-site requests.
- Max-Age or Expires: Delete the cookie after a certain period.
- Path: Define the URL path for the cookie.
Please refer to MDN for more information on each of these options.
Back in your Server Action, you can invoke the createSession()
function, and use the redirect()
API to redirect the user to the appropriate page:
Tips:
- Cookies should be set on the server to prevent client-side tampering.
- 🎥 Watch: Learn more about stateless sessions and authentication with Next.js → YouTube (11 minutes).
Updating (or refreshing) sessions
You can also extend the session's expiration time. This is useful for keeping the user logged in after they access the application again. For example:
Tip: Check if your auth library supports refresh tokens, which can be used to extend the user's session.
Deleting the session
To delete the session, you can delete the cookie:
Then you can reuse the deleteSession()
function in your application, for example, on logout:
Database Sessions
To create and manage database sessions, you'll need to follow these steps:
- Create a table in your database to store session and data (or check if your Auth Library handles this).
- Implement functionality to insert, update, and delete sessions
- Encrypt the session ID before storing it in the user's browser, and ensure the database and cookie stay in sync (this is optional, but recommended for optimistic auth checks in Middleware).
For example:
Tips:
- For faster data retrieval, consider using a database like Vercel Redis. However, you can also keep the session data in your primary database, and combine data requests to reduce the number of queries.
- You may opt to use database sessions for more advanced use cases, such as keeping track of the last time a user logged in, or number of active devices, or give users the ability to log out of all devices.
After implementing session management, you'll need to add authorization logic to control what users can access and do within your application. Continue to the Authorization section to learn more.
Authorization
Once a user is authenticated and a session is created, you can implement authorization to control what the user can access and do within your application.
There are two main types of authorization checks:
- Optimistic: Checks if the user is authorized to access a route or perform an action using the session data stored in the cookie. These checks are useful for quick operations, such as showing/hiding UI elements or redirecting users based on permissions or roles.
- Secure: Checks if the user is authorized to access a route or perform an action using the session data stored in the database. These checks are more secure and are used for operations that require access to sensitive data or actions.
For both cases, we recommend:
- Creating a Data Access Layer to centralize your authorization logic
- Using Data Transfer Objects (DTO) to only return the necessary data
- Optionally use Middleware to perform optimistic checks.
Optimistic checks with Middleware (Optional)
There are some cases where you may want to use Middleware and redirect users based on permissions:
- To perform optimistic checks. Since Middleware runs on every route, it's a good way to centralize redirect logic and pre-filter unauthorized users.
- To protect static routes that share data between users (e.g. content behind a paywall).
However, since Middleware runs on every route, including prefetched routes, it's important to only read the session from the cookie (optimistic checks), and avoid database checks to prevent performance issues.
For example:
While Middleware can be useful for initial checks, it should not be your only line of defense in protecting your data. The majority of security checks should be performed as close as possible to your data source, see Data Access Layer for more information.
Tips:
- In Middleware, you can also read cookies using
req.cookies.get('session').value
. - Middleware uses the Edge Runtime, check if your Auth library and session management library are compatible.
- You can use the
matcher
property in the Middleware to specify which routes Middleware should run on. Although, for auth, it's recommended Middleware runs on all routes.
Creating a Data Access Layer (DAL)
We recommend creating a DAL to centralize your data requests and authorization logic.
The DAL should include a function that verifies the user's session as they interact with your application. At the very least, the function should check if the session is valid, then redirect or return the user information needed to make further requests.
For example, create a separate file for your DAL that includes a verifySession()
function. Then use React's cache API to memoize the return value of the function during a React render pass:
You can then invoke the verifySession()
function in your data requests, Server Actions, Route Handlers:
Tip:
- A DAL can be used to protect data fetched at request time. However, for static routes that share data between users, data will be fetched at build time and not at request time. Use Middleware to protect static routes.
- For secure checks, you can check if the session is valid by comparing the session ID with your database. Use React's cache function to avoid unnecessary duplicate requests to the database during a render pass.
- You may wish to consolidate related data requests in a JavaScript class that runs
verifySession()
before any methods.
Using Data Transfer Objects (DTO)
When retrieving data, it's recommended you return only the necessary data that will be used in your application, and not entire objects. For example, if you're fetching user data, you might only return the user's ID and name, rather than the entire user object which could contain passwords, phone numbers, etc.
However, if you have no control over the returned data structure, or are working in a team where you want to avoid whole objects being passed to the client, you can use strategies such as specifying what fields are safe to be exposed to the client.
By centralizing your data requests and authorization logic in a DAL and using DTOs, you can ensure that all data requests are secure and consistent, making it easier to maintain, audit, and debug as your application scales.
Good to know:
- There are a couple of different ways you can define a DTO, from using
toJSON()
, to individual functions like the example above, or JS classes. Since these are JavaScript patterns and not a React or Next.js feature, we recommend doing some research to find the best pattern for your application. - Learn more about security best practices in our Security in Next.js article.
Server Components
Auth check in Server Components are useful for role-based access. For example, to conditionally render components based on the user's role:
In the example, we use the verifySession()
function from our DAL to check for 'admin', 'user', and unauthorized roles. This pattern ensures that each user interacts only with components appropriate to their role.
Layouts and auth checks
Due to Partial Rendering, be cautious when doing checks in Layouts as these don't re-render on navigation, meaning the user session won't be checked on every route change.
Instead, you should do the checks close to your data source or the component that'll be conditionally rendered.
For example, consider a shared layout that fetches the user data and displays the user image in a nav. Instead of doing the auth check in the layout, you should fetch the user data (getUser()
) in the layout and do the auth check in your DAL.
This guarantees that wherever getUser()
is called within your application, the auth check is performed, and prevents developers forgetting to check the user is authorized to access the data.
Good to know:
- A common pattern in SPAs is to
return null
in a layout or a top-level component if a user is not authorized. This pattern is not recommended since Next.js applications have multiple entry points, which will not prevent nested route segments and Server Actions from being accessed.
Server Actions
Treat Server Actions with the same security considerations as public-facing API endpoints, and verify if the user is allowed to perform a mutation.
In the example below, we check the user's role before allowing the action to proceed:
Route Handlers
Treat Route Handlers with the same security considerations as public-facing API endpoints, and verify if the user is allowed to access the Route Handler.
For example:
The example above demonstrates a Route Handler with a two-tier security check. It first checks for an active session, and then verifies if the logged-in user is an 'admin'.
Context Providers
Using context providers for auth works due to interleaving. However, React context
is not supported in Server Components, making them only applicable to Client Components.
This works, but any child Server Components will be rendered on the server first, and will not have access to the context provider’s session data:
If session data is needed in Client Components (e.g. for client-side data fetching), use React’s taintUniqueValue
API to prevent sensitive session data from being exposed to the client.
Resources
Now that you've learned about authentication in Next.js, here are Next.js-compatible libraries and resources to help you implement secure authentication and session management:
Auth Libraries
Session Management Libraries
Further Reading
To continue learning about authentication and security, check out the following resources:
- How to think about security in Next.js
- Understanding XSS Attacks
- Understanding CSRF Attacks
- The Copenhagen Book
Installation
No Description
Project Structure
No Description
Layouts and Pages
No Description
Images and Fonts
No Description
CSS and Styling
No Description
Data Fetching and Streaming
No Description
Mutating Data
No Description
Error Handling
No Description
Caching
No Description
Edge
No Description
Turbopack
No Description