Skip to content

Commit 369b1c5

Browse files
Merge branch 'main' into frederikprijck-patch-1
2 parents 4687fc4 + 16c9768 commit 369b1c5

File tree

82 files changed

+2152
-312
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2152
-312
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v4.1.0
1+
v4.4.0

CHANGELOG.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
# Change Log
22

3+
## [v4.4.0](https://github.com/auth0/nextjs-auth0/tree/v4.4.0) (2025-04-01)
4+
[Full Changelog](https://github.com/auth0/nextjs-auth0/compare/v4.3.0...v4.4.0)
5+
6+
**Added**
7+
- Add note about access-token endpoint to README [\#2020](https://github.com/auth0/nextjs-auth0/pull/2020) ([frederikprijck](https://github.com/frederikprijck))
8+
- Add support for Connection Access Token [\#2010](https://github.com/auth0/nextjs-auth0/pull/2010) ([frederikprijck](https://github.com/frederikprijck))
9+
10+
**Fixed**
11+
- fix: Delete legacy cookie once v4 cookie is set [\#2019](https://github.com/auth0/nextjs-auth0/pull/2019) ([frederikprijck](https://github.com/frederikprijck))
12+
- fix: Ensure to delete cookies when switching from single to chunks and vica versa [\#2013](https://github.com/auth0/nextjs-auth0/pull/2013) ([frederikprijck](https://github.com/frederikprijck))
13+
- fix: Clean up cookie chunks when cookie size shrinks [\#2014](https://github.com/auth0/nextjs-auth0/pull/2014) ([frederikprijck](https://github.com/frederikprijck))
14+
- fix: use NEXT_PUBLIC_PROFILE_ROUTE in Auth0Provider [\#2021](https://github.com/auth0/nextjs-auth0/pull/2021) ([tusharpandey13](https://github.com/tusharpandey13))
15+
- fix: Ensure to pass-through enableAccessTokenEndpoint [\#2015](https://github.com/auth0/nextjs-auth0/pull/2015) ([frederikprijck](https://github.com/frederikprijck))
16+
- fix: Remove obsolete warning about cookie-size [\#2012](https://github.com/auth0/nextjs-auth0/pull/2012) ([frederikprijck](https://github.com/frederikprijck))
17+
18+
## [v4.3.0](https://github.com/auth0/nextjs-auth0/tree/v4.3.0) (2025-03-28)
19+
[Full Changelog](https://github.com/auth0/nextjs-auth0/compare/v4.2.1...v4.3.0)
20+
21+
**Added**
22+
- Access Token Exposure Control [\#1979](https://github.com/auth0/nextjs-auth0/pull/1979) ([tusharpandey13](https://github.com/tusharpandey13))
23+
- Cookie chunking support [\#1975](https://github.com/auth0/nextjs-auth0/pull/1975) ([tusharpandey13](https://github.com/tusharpandey13))
24+
- Add idToken to TokenSet in SessionData [\#1978](https://github.com/auth0/nextjs-auth0/pull/1978) ([tusharpandey13](https://github.com/tusharpandey13))
25+
26+
## [v4.2.1](https://github.com/auth0/nextjs-auth0/tree/v4.2.1) (2025-03-24)
27+
[Full Changelog](https://github.com/auth0/nextjs-auth0/compare/v4.2.0...v4.2.1)
28+
29+
**Changed**
30+
- Bump next in SDK as well as examples [\#1992](https://github.com/auth0/nextjs-auth0/pull/1992) ([frederikprijck](https://github.com/frederikprijck))
31+
32+
## [v4.2.0](https://github.com/auth0/nextjs-auth0/tree/v4.2.0) (2025-03-23)
33+
[Full Changelog](https://github.com/auth0/nextjs-auth0/compare/v4.1.0...v4.2.0)
34+
35+
**Security**
36+
- Enforce nextjs peerDependency to 14.2.25 and 15.2.3 [\#1988](https://github.com/auth0/nextjs-auth0/pull/1988) ([frederikprijck](https://github.com/frederikprijck))
37+
38+
The above security fix was done to help prevent customers being vulnerable to [Authorization Bypass in Next.js Middleware](https://github.com/advisories/GHSA-f82v-jwr5-mffw).
39+
340
## [v4.1.0](https://github.com/auth0/nextjs-auth0/tree/v4.1.0) (2025-03-13)
441
[Full Changelog](https://github.com/auth0/nextjs-auth0/compare/v4.0.3...v4.1.0)
542

EXAMPLES.md

Lines changed: 204 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
- [Custom routes](#custom-routes)
3333
- [Testing helpers](#testing-helpers)
3434
- [`generateSessionCookie`](#generatesessioncookie)
35+
- [Programmatically starting interactive login](#programmatically-starting-interactive-login)
36+
- [Passing authorization parameters](#passing-authorization-parameters-1)
37+
- [The `returnTo` parameter](#the-returnto-parameter-1)
38+
- [Redirecting the user after authentication](#redirecting-the-user-after-authentication-1)
39+
- [Getting access tokens for connections](#getting-access-tokens-for-connections)
40+
- [On the server (App Router)](#on-the-server-app-router-3)
41+
- [On the server (Pages Router)](#on-the-server-pages-router-3)
42+
- [Middleware](#middleware-3)
3543

3644
## Passing authorization parameters
3745

@@ -60,14 +68,18 @@ The `returnTo` parameter can be appended to the login to specify where you would
6068

6169
For example: `/auth/login?returnTo=/dashboard` would redirect the user to the `/dashboard` route after they have authenticated.
6270

71+
> [!NOTE]
72+
> The URL specified as `returnTo` parameters must be registered in your client's **Allowed Callback URLs**.
73+
74+
6375
### Redirecting the user after logging out
6476

6577
The `returnTo` parameter can be appended to the logout to specify where you would like to redirect the user after they have logged out.
6678

6779
For example: `/auth/login?returnTo=https://example.com/some-page` would redirect the user to the `https://example.com/some-page` URL after they have logged out.
6880

6981
> [!NOTE]
70-
> The URLs specified as `returnTo` parameters must be registered in your client's **Allowed Logout URLs**.
82+
> The URL specified as `returnTo` parameters must be registered in your client's **Allowed Logout URLs**.
7183
7284
## Accessing the authenticated user
7385

@@ -185,6 +197,15 @@ export async function middleware(request: NextRequest) {
185197
> [!IMPORTANT]
186198
> The `request` object must be passed as a parameter to the `getSession(request)` method when called from a middleware to ensure that any updates to the session can be read within the same request.
187199
200+
## Accessing the idToken
201+
`idToken` can be accessed from the session in the following way:
202+
203+
```js
204+
const session = await auth0.getSession();
205+
const idToken = session.tokenSet.idToken;
206+
```
207+
208+
188209
## Updating the session
189210

190211
The `updateSession` method could be used to update the session of the currently authenticated user in the App Router, Pages Router, and middleware. If the user does not have a session, an error will be thrown.
@@ -753,32 +774,200 @@ const sessionCookieValue = await generateSessionCookie(
753774
)
754775
```
755776

777+
## Programmatically starting interactive login
756778

757-
## Programmatic Pushed Authentication Requests (PAR)
758-
759-
The method `startInteractiveLogin` can be called with authorizationParams to initiate an interactive login flow.
760-
The code collects authorization parameters on the server side rather than constructing them directly in the browser.
779+
Additionally to the ability to initialize the interactive login process by redirecting the user to the built-in `auth/login` endpoint,
780+
the `startInteractiveLogin` method can also be called programmatically.
761781

762782
```typescript
763-
// app/api/auth/login/route.ts
764783
import { auth0 } from "./lib/auth0";
765784
import { NextRequest } from "next/server";
766785

767786
export const GET = async (req: NextRequest) => {
768-
// Extract custom parameters from request URL if needed
769-
const searchParams = Object.fromEntries(req.nextUrl.searchParams.entries());
787+
return auth0.startInteractiveLogin();
788+
};
789+
```
790+
791+
### Passing authorization parameters
792+
793+
There are 2 ways to customize the authorization parameters that will be passed to the `/authorize` endpoint when calling `startInteractiveLogin` programmatically. The first option is through static configuration when instantiating the client, like so:
794+
795+
```ts
796+
export const auth0 = new Auth0Client({
797+
authorizationParameters: {
798+
scope: "openid profile email",
799+
audience: "urn:custom:api",
800+
},
801+
});
802+
```
803+
804+
The second option is by configuring `authorizationParams` when calling `startInteractiveLogin`:
770805

806+
```ts
807+
import { auth0 } from "./lib/auth0";
808+
import { NextRequest } from "next/server";
809+
810+
export const GET = async (req: NextRequest) => {
771811
// Call startInteractiveLogin with optional parameters
772812
return auth0.startInteractiveLogin({
773-
// a custom returnTo URL can be specified
774-
returnTo: "/dashboard",
775813
authorizationParameters: {
776-
prompt: searchParams.prompt,
777-
login_hint: searchParams.login_hint,
778-
// Add any custom auth parameters if required
779-
audience: "custom-audience"
814+
scope: "openid profile email",
815+
audience: "urn:custom:api",
780816
}
781817
});
782818
};
819+
```
820+
821+
## The `returnTo` parameter
822+
823+
### Redirecting the user after authentication
824+
825+
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.
826+
827+
```ts
828+
import { auth0 } from "./lib/auth0";
829+
import { NextRequest } from "next/server";
830+
831+
export const GET = async (req: NextRequest) => {
832+
return auth0.startInteractiveLogin({
833+
returnTo: '/dashboard',
834+
});
835+
};
836+
```
837+
838+
> [!NOTE]
839+
> The URLs specified as `returnTo` parameters must be registered in your client's **Allowed Callback URLs**.
840+
841+
842+
## Getting access tokens for connections
843+
You can retrieve an access token for a connection using the `getAccessTokenForConnection()` method, which accepts an object with the following properties:
844+
- `connection`: The federated connection for which an access token should be retrieved.
845+
- `login_hint`: The optional login_hint parameter to pass to the `/authorize` endpoint.
846+
847+
### On the server (App Router)
848+
849+
On the server, the `getAccessTokenForConnection()` helper can be used in Server Routes, Server Actions and Server Components to get an access token for a connection.
850+
851+
> [!IMPORTANT]
852+
> Server Components cannot set cookies. Calling `getAccessTokenForConnection()` in a Server Component will cause the access token to be refreshed, if it is expired, and the updated token set will not to be persisted.
853+
>
854+
> It is recommended to call `getAccessTokenForConnection(req, res)` in the middleware if you need to refresh the token in a Server Component as this will ensure the token is refreshed and correctly persisted.
855+
856+
For example:
857+
858+
```ts
859+
import { NextResponse } from "next/server"
860+
861+
import { auth0 } from "@/lib/auth0"
862+
863+
export async function GET() {
864+
try {
865+
const token = await auth0.getAccessTokenForConnection({ connection: 'google-oauth2' })
866+
// call external API with token...
867+
} catch (err) {
868+
// err will be an instance of AccessTokenError if an access token could not be obtained
869+
}
870+
871+
return NextResponse.json({
872+
message: "Success!",
873+
})
874+
}
875+
```
876+
877+
Upon further calls for the same provider, the cached value will be used until it expires.
878+
879+
### On the server (Pages Router)
880+
881+
On the server, the `getAccessTokenForConnection({}, req, res)` helper can be used in `getServerSideProps` and API routes to get an access token for a connection, like so:
882+
883+
```ts
884+
import type { NextApiRequest, NextApiResponse } from "next"
885+
886+
import { auth0 } from "@/lib/auth0"
783887

784-
```
888+
export default async function handler(
889+
req: NextApiRequest,
890+
res: NextApiResponse<{ message: string }>
891+
) {
892+
try {
893+
const token = await auth0.getAccessTokenForConnection({ connection: 'google-oauth2' }, req, res)
894+
} catch (err) {
895+
// err will be an instance of AccessTokenError if an access token could not be obtained
896+
}
897+
898+
res.status(200).json({ message: "Success!" })
899+
}
900+
```
901+
902+
### Middleware
903+
904+
In middleware, the `getAccessTokenForConnection({}, req, res)` helper can be used to get an access token for a connection, like so:
905+
906+
```tsx
907+
import { NextRequest, NextResponse } from "next/server"
908+
909+
import { auth0 } from "@/lib/auth0"
910+
911+
export async function middleware(request: NextRequest) {
912+
const authRes = await auth0.middleware(request)
913+
914+
if (request.nextUrl.pathname.startsWith("/auth")) {
915+
return authRes
916+
}
917+
918+
const session = await auth0.getSession(request)
919+
920+
if (!session) {
921+
// user is not authenticated, redirect to login page
922+
return NextResponse.redirect(new URL("/auth/login", request.nextUrl.origin))
923+
}
924+
925+
const accessToken = await auth0.getAccessTokenForConnection({ connection: 'google-oauth2' }, request, authRes)
926+
927+
// the headers from the auth middleware should always be returned
928+
return authRes
929+
}
930+
```
931+
932+
> [!IMPORTANT]
933+
> The `request` and `response` objects must be passed as a parameters to the `getAccessTokenForConnection({}, request, response)` method when called from a middleware to ensure that the refreshed access token can be accessed within the same request.
934+
935+
If you are using the Pages Router and are calling the `getAccessTokenForConnection` method in both the middleware and an API Route or `getServerSideProps`, it's recommended to propagate the headers from the middleware, as shown below. This will ensure that calling `getAccessTokenForConnection` in the API Route or `getServerSideProps` will not result in the access token being refreshed again.
936+
937+
```ts
938+
import { NextRequest, NextResponse } from "next/server"
939+
940+
import { auth0 } from "@/lib/auth0"
941+
942+
export async function middleware(request: NextRequest) {
943+
const authRes = await auth0.middleware(request)
944+
945+
if (request.nextUrl.pathname.startsWith("/auth")) {
946+
return authRes
947+
}
948+
949+
const session = await auth0.getSession(request)
950+
951+
if (!session) {
952+
// user is not authenticated, redirect to login page
953+
return NextResponse.redirect(new URL("/auth/login", request.nextUrl.origin))
954+
}
955+
956+
const accessToken = await auth0.getAccessTokenForConnection({ connection: 'google-oauth2' }, request, authRes)
957+
958+
// create a new response with the updated request headers
959+
const resWithCombinedHeaders = NextResponse.next({
960+
request: {
961+
headers: request.headers,
962+
},
963+
})
964+
965+
// set the response headers (set-cookie) from the auth response
966+
authRes.headers.forEach((value, key) => {
967+
resWithCombinedHeaders.headers.set(key, value)
968+
})
969+
970+
// the headers from the auth middleware should always be returned
971+
return resWithCombinedHeaders
972+
}
973+
```

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,24 @@ You can customize the client by using the options below:
151151

152152
The SDK mounts 6 routes:
153153

154-
1. `/auth/login`: the login route that the user will be redirected to to start a initiate an authentication transaction
155-
2. `/auth/logout`: the logout route that must be addedto your Auth0 application's Allowed Logout URLs
156-
3. `/auth/callback`: the callback route that must be addedto your Auth0 application's Allowed Callback URLs
154+
1. `/auth/login`: the login route that the user will be redirected to to initiate an authentication transaction
155+
2. `/auth/logout`: the logout route that must be added to your Auth0 application's Allowed Logout URLs
156+
3. `/auth/callback`: the callback route that must be added to your Auth0 application's Allowed Callback URLs
157157
4. `/auth/profile`: the route to check the user's session and return their attributes
158158
5. `/auth/access-token`: the route to check the user's session and return an access token (which will be automatically refreshed if a refresh token is available)
159159
6. `/auth/backchannel-logout`: the route that will receive a `logout_token` when a configured Back-Channel Logout initiator occurs
160160

161+
> [!IMPORTANT]
162+
> The `/auth/access-token` route is enabled by default, but is only neccessary when the access token is needed in the client. If this isn't something you need, you can disable this endpoint by setting `enableAccessTokenEndpoint` to `false`.
163+
161164
## Feedback
162165

163166
### Contributing
164167

165168
We appreciate feedback and contribution to this repo! Before you get started, please read the following:
166169

167170
- [Auth0's general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md)
168-
- [Auth0's code of conduct guidelines](https://github.com/auth0/express-openid-connect/blob/master/CODE-OF-CONDUCT.md)
171+
- [Auth0's code of conduct guidelines](https://github.com/auth0/nextjs-auth0/blob/main/CODE-OF-CONDUCT.md)
169172
- [This repo's contribution guide](./CONTRIBUTING.md)
170173

171174
### Raise an issue
@@ -189,5 +192,5 @@ Please do not report security vulnerabilities on the public GitHub issue tracker
189192
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href="https://auth0.com/why-auth0">Why Auth0?</a>
190193
</p>
191194
<p align="center">
192-
This project is licensed under the MIT license. See the <a href="https://github.com/auth0/express-openid-connect/blob/master/LICENSE"> LICENSE</a> file for more info.
195+
This project is licensed under the MIT license. See the <a href="https://github.com/auth0/nextjs-auth0/blob/main/LICENSE"> LICENSE</a> file for more info.
193196
</p>

V4_MIGRATION_GUIDE.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,6 @@ If you'd like to customize the `user` object to include additional custom claims
235235
## Additional changes
236236

237237
- By default, v4 is edge-compatible and as such there is no longer a `@auth0/nextjs-auth0/edge` export.
238-
- Cookie chunking has been removed
239-
- If the cookie size exceeds the browser limit of 4096 bytes, a warning will be logged
240-
- To store large session data, please use a [custom data store](https://github.com/auth0/nextjs-auth0/tree/main?tab=readme-ov-file#database-sessions) with a SessionStore implementation
241238
- All cookies set by the SDK default to `SameSite=Lax`
242239
- `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).
243240
- `getAccessToken` can now be called in React Server Components.

docs/assets/hierarchy.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/assets/navigation.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)