Skip to main content
This guide will help you migrate from 3.x to 4.x of the Auth0 Next.js SDK.

Environment Variables

The following environment variables are required in v4:
Breaking ChangesOf the required variables, the following have changed from v3:
  • AUTH0_BASE_URL has been renamed to APP_BASE_URL (e.g.: http://localhost:3000)
  • AUTH0_ISSUER_BASE_URL has been renamed to AUTH0_DOMAIN and does not accept a scheme (e.g.: example.us.auth0.com)
All other configuration must be specified via the Auth0Client constructor.
In v3 the audience parameter could be specified via the AUTH0_AUDIENCE environment variable. In v4, the audience parameter must be specified as a query parameter or via the authorizationParameters configuration option. For more information on how to pass custom parameters in v4, please see Passing custom authorization parameters.

Routes

Previously, it was required to set up a dynamic Route Handler to mount the authentication endpoints to handle requests. For example, in v3 when using the App Router, you were required to create a Route Handler, under /app/api/auth/[auth0]/route.ts, with the following contents:
In v4, the routes are now mounted automatically by the middleware:
For a complete example, see the Getting Started guide.
Route Path ChangesIn v4, the mounted routes drop the /api prefix. For example, the default login route is now /auth/login instead of /api/auth/login. To link to the login route, it would now be:
If you are using an existing client, you will need to update your Allowed Callback URLs accordingly.
The complete list of routes mounted by the SDK can be found in the main documentation.

The Auth0 Middleware

In v4, the Auth0 middleware is a central component of the SDK. It serves a number of core functions such as registering the required authentication endpoints, providing rolling sessions functionality, keeping access tokens fresh, etc. When configuring your application to use v4 of the SDK, it is now required to mount the middleware:
The above middleware is a basic setup. It passes incoming requests to the Auth0 SDK’s request handler, which in turn manages the default auto-mounted authentication routes, user sessions, and the overall authentication flow. It does not protect any routes by default. To protect routes from unauthenticated users, read the section below on protecting routes.

Protecting Routes

By default, the middleware does not protect any routes. To protect a page, you can use the getSession() handler in the middleware, like so:
We recommend keeping the security checks as close as possible to the data source you’re accessing. This is also in-line with the recommendations from the Next.js team.

Combining with Other Middleware

For scenarios where you need to combine the Auth0 middleware with other Next.js middleware, please refer to the Combining Middleware guide for examples and best practices.

Migrating <UserProvider /> to <Auth0Provider />

The <UserProvider /> has been renamed to <Auth0Provider />.
Previously, when setting up your application to use v3 of the SDK, it was required to wrap your layout in the <UserProvider />. This is no longer required by default. If you would like to pass an initial user during server rendering to be available to the useUser() hook, you can wrap your components with the new <Auth0Provider />.

Rolling Sessions

In v4, rolling sessions are enabled by default and are handled automatically by the middleware with no additional configuration required. See the session configuration section for additional details on how to configure it.

Migrating from withPageAuthRequired and withApiAuthRequired

withPageAuthRequired and withApiAuthRequired have been removed from v4 of the SDK.
Instead, we recommend adding a getSession() check or relying on useUser() hook where you would have previously used the helpers.

Server-Side Authentication Check

On the server-side, the getSession() method can be used to check if the user is authenticated:
The getSession() method can be used in the App Router in Server Components, Server Routes (APIs), Server Actions, and middleware. In the Pages Router, the getSession(req) method takes a request object and can be used in getServerSideProps, API routes, and middleware.

Client-Side Authentication Check

In the browser, you can rely on the useUser() hook to check if the user is authenticated:

Passing Custom Authorization Parameters

In v4, you can simply append the authorization parameters to the query parameter of the login endpoint and they will be automatically forwarded to the /authorize endpoint:
In previous versions, authParams was used. In v4, use authorizationParameters. For example, for silent authentication: authorizationParameters: { prompt: 'none' }.

ID Token Claims

In v3, any claims added to the ID token were automatically propagated to the user object in the session. This resulted in large cookies that exceeded browser limits.
Breaking Change: Default ClaimsIn v4, by default, only the following claims are persisted in the user object of session:
  • sub
  • name
  • nickname
  • given_name
  • family_name
  • picture
  • email
  • email_verified
  • org_id
If you’d like to customize the user object to include additional custom claims from the ID token, you can use the beforeSessionSaved hook. For a list of default claims included in the user object, refer to the ID Token claims and the user object section in the Examples guide.

Handling Dynamic Base URLs

When deploying to platforms like Vercel with dynamic preview URLs, it’s important to set the correct appBaseUrl and redirect_uri at runtime — especially in preview environments where URLs change per deployment.
1

Set APP_BASE_URL dynamically in next.config.js

2

Use the APP_BASE_URL in your Auth0 configuration

3

Update Auth0 application settings

Ensure your Auth0 application settings include the dynamic URL in the Allowed Callback URLs and Allowed Logout URLs fields. For example, https://*.vercel.app/auth/callback.

Additional Changes

Edge Compatibility

By default, v4 is edge-compatible and as such there is no longer a @auth0/nextjs-auth0/edge export. All cookies set by the SDK default to SameSite=Lax. For details on how to customize cookie attributes, see the Cookie Configuration section in the Examples guide.

Touch Session Removed

touchSession method was removed. The middleware enables rolling sessions by default and can be configured via the Session configuration section in the Examples guide.

Access Token in React Server Components

getAccessToken can now be called in React Server Components. For examples on how to use getAccessToken in various environments (browser, App Router, Pages Router, Middleware), refer to the Getting an access token section in the Examples guide.

Logout Behavior

By default, v4 will use OpenID Connect’s RP-Initiated Logout if it’s enabled on the tenant. Otherwise, it will fallback to the /v2/logout endpoint.

Profile Route 401 Response

The v4 /auth/profile profile route returns a 401 Unauthorized error when unauthenticated. If you would like to replicate the v3 behaviour where the profile route returns a 204 response, enable noContentProfileResponseWhenUnauthenticated in Auth0ClientOptions. This prevents the SDK from automatically retrying the error when the user is logged out.

Customizing Auth Handlers

In v3, you could customize individual auth handlers by providing custom implementations to the handleAuth function:
In v4, the auth routes are handled automatically by the middleware, but you can achieve similar customization through two main approaches:

Run Custom Code Before Auth Handlers (Middleware Interception)

You can intercept auth routes in your middleware to run custom logic before the auth handlers execute:

Run Code After Authentication (Callback Hook)

Use the onCallback hook to add custom logic after authentication completes:

Additional Customization Options

  • Login parameters: Use query parameters (/auth/login?audience=...) or static configuration
  • Session data: Use the beforeSessionSaved hook to modify session data
  • Logout redirects: Use query parameters (/auth/logout?returnTo=...)
  • Transaction cookies: Configure transaction cookie behavior with TransactionStore options. See Transaction Cookie Configuration for details.
Always validate redirect URLs to prevent open redirect attacks. Use relative URLs when possible.
For detailed examples and implementation patterns, see Customizing Auth Handlers in the Examples guide. V4 introduces improved transaction cookie management to prevent cookie accumulation issues that could cause HTTP 413 errors. The TransactionStore now supports configurable parallel transactions to control whether multiple login flows can run simultaneously.
In contrast, V3 did not support parallel transactions.