You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When calling `startInteractiveLogin`, the `returnTo` parameter can be configured to specify where you would like to redirect the user to after they have completed their authentication and have returned to your application.
959
959
960
960
```ts
961
-
import { auth0 } from"./lib/auth0";
961
+
import { auth0 } from"./lib/auth0";// Adjust path if your auth0 client is elsewhere
962
962
import { NextRequest } from"next/server";
963
963
964
964
exportconst GET =async (req:NextRequest) => {
@@ -991,7 +991,7 @@ For example:
991
991
```ts
992
992
import { NextResponse } from"next/server"
993
993
994
-
import { auth0 } from"@/lib/auth0"
994
+
import { auth0 } from"./lib/auth0"// Adjust path if your auth0 client is elsewhere
995
995
996
996
exportasyncfunction GET() {
997
997
try {
@@ -1016,7 +1016,7 @@ On the server, the `getAccessTokenForConnection({}, req, res)` helper can be use
@@ -57,7 +57,7 @@ Additionally, in v4, the mounted routes drop the `/api` prefix. For example, the
57
57
58
58
The complete list of routes mounted by the SDK can be found [here](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#routes).
59
59
60
-
## Auth0 middleware
60
+
## The Auth0 middleware
61
61
62
62
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.
63
63
@@ -68,10 +68,10 @@ When configuring your application to use v4 of the SDK, it is now **required** t
68
68
69
69
importtype { NextRequest } from"next/server"
70
70
71
-
import { auth0 } from"./lib/auth0"
71
+
import { auth0 } from"./lib/auth0"// Adjust path if your auth0 client is elsewhere
returnawaitauth0.middleware(request)// Returns a NextResponse object
75
75
}
76
76
77
77
exportconst config = {
@@ -86,6 +86,8 @@ export const config = {
86
86
],
87
87
}
88
88
```
89
+
> [!NOTE]
90
+
> 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](https://github.com/auth0/nextjs-auth0/blob/main/README.md#routes), user sessions, and the overall authentication flow. It does **not** protect any routes by default, in order to protect routes from unauthenticated users, read the section below on [protecting routes](https://github.com/auth0/nextjs-auth0/blob/main/V4_MIGRATION_GUIDE.md#protecting-routes).
89
91
90
92
See [the Getting Started section](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#getting-started) for details on how to configure the middleware.
91
93
@@ -94,56 +96,73 @@ See [the Getting Started section](https://github.com/auth0/nextjs-auth0/tree/mai
94
96
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](https://nextjs.org/docs/app/building-your-application/authentication#optimistic-checks-with-middleware-optional).
119
129
120
-
## `<UserProvider />`
130
+
131
+
### Combining with other middleware
132
+
133
+
For scenarios where you need to combine the Auth0 middleware with other Next.js middleware, please refer to the [Combining middleware](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#combining-middleware) guide for examples and best practices.
134
+
135
+
## Migrating `<UserProvider />` to `<Auth0Provider />`
121
136
122
137
The `<UserProvider />` has been renamed to `<Auth0Provider />`.
123
138
124
139
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.**
125
140
126
-
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 />` ([see example](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#auth0provider-)).
141
+
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 />` ([see example](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#auth0provider-)).
127
142
128
143
## Rolling sessions
129
144
130
145
In v4, rolling sessions are enabled by default and are handled automatically by the middleware with no additional configuration required.
131
146
132
-
See the [session configuration section](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#session-configuration) for additional details on how to configure it.
147
+
See the [session configuration section](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#session-configuration) for additional details on how to configure it.
133
148
134
-
## `withPageAuthRequired` and `withApiAuthRequired`
149
+
## Migrating from `withPageAuthRequired` and `withApiAuthRequired`
135
150
136
151
`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.
137
152
138
153
On the server-side, the `getSession()` method can be used to check if the user is authenticated:
139
154
140
155
```tsx
141
-
function Page() {
142
-
const session =awaitgetSession()
156
+
// Example for an App Router Server Component
157
+
import { redirect } from'next/navigation'
158
+
import { auth0 } from'./lib/auth0'// Adjust path if your auth0 client is elsewhere
159
+
160
+
exportdefaultasyncfunction Page() {
161
+
const session =awaitauth0.getSession()
143
162
144
163
if (!session) {
145
-
//the user will be redirected to authenticate and then taken to the
146
-
// /dashboard route after successfully being authenticated
164
+
//The user will be redirected to authenticate and then taken to the
165
+
// /dashboard route after successfully being authenticated.
147
166
returnredirect('/auth/login?returnTo=/dashboard')
148
167
}
149
168
@@ -155,7 +174,7 @@ The `getSession()` method can be used in the App Router in Server Components, Se
155
174
156
175
In the Pages Router, the `getSession(req)` method takes a request object and can be used in `getServerSideProps`, API routes, and middleware.
157
176
158
-
Read more about [accessing the authenticated user here](https://github.com/guabu/nextjs-auth0/tree/main?tab=readme-ov-file#accessing-the-authenticated-user).
177
+
Read more about [accessing the authenticated user in various contexts (browser, server, middleware) in the Examples guide](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#accessing-the-authenticated-user).
159
178
160
179
In the browser, you can rely on the `useUser()` hook to check if the user is authenticated. For example:
161
180
@@ -212,7 +231,7 @@ export const auth0 = new Auth0Client({
212
231
})
213
232
```
214
233
215
-
Read more about [passing authorization parameters](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#passing-authorization-parameters).
234
+
Read more about [passing authorization parameters](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#passing-authorization-parameters).
216
235
217
236
## ID token claims
218
237
@@ -231,11 +250,38 @@ In v4, by default, the only claims that are persisted in the `user` object of se
231
250
-`org_id`
232
251
233
252
If you'd like to customize the `user` object to include additional custom claims from the ID token, you can use the `beforeSessionSaved` hook (see [beforeSessionSaved hook](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#beforesessionsaved))
253
+
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](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#id-token-claims-and-the-user-object).
254
+
255
+
## Handling Dynamic Base URLs (e.g. Vercel Preview Deployments)
256
+
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.
257
+
1. Set `APP_BASE_URL` dynamically in `next.config.js`:
258
+
```ts
259
+
// next.config.js
260
+
module.exports= {
261
+
env: {
262
+
APP_BASE_URL:
263
+
process.env.VERCEL_ENV==="preview"
264
+
?`https://${process.env.VERCEL_BRANCH_URL}`
265
+
:process.env.APP_BASE_URL,
266
+
},
267
+
};
268
+
```
269
+
2. Use the `APP_BASE_URL` in your Auth0 configuration:
3. 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`.
234
280
235
281
## Additional changes
236
282
237
283
- By default, v4 is edge-compatible and as such there is no longer a `@auth0/nextjs-auth0/edge` export.
238
-
- All cookies set by the SDK default to `SameSite=Lax`
239
-
-`touchSession` method was removed. The middleware enables rolling sessions by default and can be configured via the [session configuration](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#session-configuration).
240
-
-`getAccessToken` can now be called in React Server Components.
241
-
- By default, v4 will use [OpenID Connect's RP-Initiated Logout](https://auth0.com/docs/authenticate/login/logout/log-users-out-of-auth0) if it's enabled on the tenant. Otherwise, it will fallback to the `/v2/logout` endpoint.
284
+
- 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](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#cookie-configuration).
285
+
-`touchSession` method was removed. The middleware enables rolling sessions by default and can be configured via the [Session configuration section in the Examples guide](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#session-configuration).
286
+
-`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](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#getting-an-access-token).
287
+
- By default, v4 will use [OpenID Connect's RP-Initiated Logout](https://auth0.com/docs/authenticate/login/logout/log-users-out-of-auth0) if it's enabled on the tenant. Otherwise, it will fallback to the `/v2/logout` endpoint.
0 commit comments