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
returnawaitauth0.middleware(request) // Returns a NextResponse object
@@ -87,7 +87,7 @@ export const config = {
87
87
}
88
88
```
89
89
> [!NOTE]
90
-
> The above middleware is a basic setup. Its primary function is to pass incoming requests to the Auth0 SDK's request handler, which in turn manages the [default auto-mounted authentication routes](../README.md#routes), user sessions, and the overall authentication flow.
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).
91
91
92
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.
93
93
@@ -99,7 +99,7 @@ By default, **the middleware does not protect any routes**. To protect a page, y
99
99
exportasyncfunction middleware(request) {
100
100
const authRes =awaitauth0.middleware(request); // Returns a NextResponse object
101
101
102
-
// Ensure our own middleware does not handle the `/auth` routes, auto-mounted and handled by the SDK
102
+
// Ensure your own middleware does not handle the `/auth` routes, auto-mounted and handled by the SDK
103
103
if (request.nextUrl.pathname.startsWith("/auth")) {
104
104
returnauthRes;
105
105
}
@@ -109,6 +109,7 @@ export async function middleware(request) {
109
109
returnauthRes;
110
110
}
111
111
112
+
// Any route that gets to this point will be considered a protected route, and require the user to be logged-in to be able to access it
112
113
const { origin } =newURL(request.url)
113
114
const session =awaitauth0.getSession()
114
115
@@ -126,25 +127,25 @@ export async function middleware(request) {
126
127
> [!NOTE]
127
128
> 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).
128
129
129
-
For more examples on accessing user sessions in middleware, see [Accessing the authenticated user in Middleware in the Examples guide](./EXAMPLES.md#middleware).
130
+
For more examples on accessing user sessions in middleware, see [Accessing the authenticated user in Middleware in the Examples guide](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#middleware).
130
131
131
132
### Combining with other middleware
132
133
133
-
For scenarios where you need to combine the Auth0 middleware with other Next.js middleware, please refer to the [Combining middleware](../EXAMPLES.md#combining-middleware) guide for examples and best practices.
134
+
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.
The `<UserProvider />` has been renamed to `<Auth0Provider />`.
138
139
139
140
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.**
140
141
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/tree/main?tab=readme-ov-file#auth0provider-)).
142
+
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-)).
142
143
143
144
## Rolling sessions
144
145
145
146
In v4, rolling sessions are enabled by default and are handled automatically by the middleware with no additional configuration required.
146
147
147
-
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.
148
+
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.
148
149
149
150
## Migrating from `withPageAuthRequired` and `withApiAuthRequired`
150
151
@@ -174,7 +175,7 @@ The `getSession()` method can be used in the App Router in Server Components, Se
174
175
175
176
In the Pages Router, the `getSession(req)` method takes a request object and can be used in `getServerSideProps`, API routes, and middleware.
176
177
177
-
Read more about [accessing the authenticated user in various contexts (browser, server, middleware) in the Examples guide](./EXAMPLES.md#accessing-the-authenticated-user).
178
+
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).
178
179
179
180
In the browser, you can rely on the `useUser()` hook to check if the user is authenticated. For example:
180
181
@@ -231,7 +232,7 @@ export const auth0 = new Auth0Client({
231
232
})
232
233
```
233
234
234
-
Read more about [passing authorization parameters](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#passing-authorization-parameters).
235
+
Read more about [passing authorization parameters](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#passing-authorization-parameters).
235
236
236
237
## ID token claims
237
238
@@ -250,7 +251,7 @@ In v4, by default, the only claims that are persisted in the `user` object of se
250
251
-`org_id`
251
252
252
253
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](./EXAMPLES.md#id-token-claims-and-the-user-object).
254
+
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
255
256
## Handling Dynamic Base URLs (e.g. Vercel Preview Deployments)
256
257
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.
@@ -281,7 +282,7 @@ export const auth0 = new Auth0Client({
281
282
## Additional changes
282
283
283
284
- By default, v4 is edge-compatible and as such there is no longer a `@auth0/nextjs-auth0/edge` export.
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](./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](./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](./EXAMPLES.md#getting-an-access-token).
285
+
- 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).
286
+
-`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).
287
+
-`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
288
- 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