Skip to content

Commit 93e7b2a

Browse files
feat: Add documentation for proactive token refresh and session-only auth (#2273)
1 parent 803de07 commit 93e7b2a

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

EXAMPLES.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ export default function Profile() {
162162

163163
On the server, the `getSession()` helper can be used in Server Components, Server Routes, and Server Actions to get the session of the currently authenticated user and to protect resources, like so:
164164

165+
> [!NOTE]
166+
> The `getSession()` method returns a complete session object containing the user profile and all available tokens (access token, ID token, and refresh token when present). Use this method for applications that only need user identity information without calling external APIs, as it provides access to the user's profile data from the ID token without requiring additional API calls. This approach is suitable for session-only authentication patterns.
167+
For API access, use `getAccessToken()` to get an access token, this handles automatic token refresh.
168+
165169
```tsx
166170
import { auth0 } from "@/lib/auth0";
167171

@@ -321,7 +325,7 @@ Then you can access your API from the frontend with a valid session cookie.
321325

322326
```jsx
323327
// pages/products
324-
import { withPageAuthRequired } from "@auth0/nextjs-auth0/client";
328+
import { withPageAuthRequired } from "@auth0/nextjs-auth0";
325329
import useSWR from "swr";
326330

327331
const fetcher = async (uri) => {
@@ -358,7 +362,7 @@ Then you can access your API from the frontend with a valid session cookie.
358362
// app/products/page.jsx
359363
"use client";
360364

361-
import { withPageAuthRequired } from "@auth0/nextjs-auth0/client";
365+
import { withPageAuthRequired } from "@auth0/nextjs-auth0";
362366
import useSWR from "swr";
363367

364368
const fetcher = async (uri) => {
@@ -547,6 +551,9 @@ export async function middleware(request: NextRequest) {
547551

548552
The `getAccessToken()` helper can be used both in the browser and on the server to obtain the access token to call external APIs. If the access token has expired and a refresh token is available, it will automatically be refreshed and persisted.
549553

554+
> [!IMPORTANT]
555+
> **Refresh Token Rotation**: If your Auth0 application uses Refresh Token Rotation, configure an overlap period to prevent race conditions when multiple requests attempt to refresh tokens simultaneously. This can be configured in your Auth0 Dashboard under Applications > Advanced Settings > OAuth, or disable rotation entirely for server-side applications that don't require it.
556+
550557
### In the browser
551558

552559
To obtain an access token to call an external API on the client, you can use the `getAccessToken()` helper, like so:
@@ -767,6 +774,21 @@ export default withApiAuthRequired(async function handler(
767774
By setting `{ refresh: true }`, you instruct the SDK to bypass the standard expiration check and request a new access token from the identity provider using the refresh token (if available and valid). The new token set (including the potentially updated access token, refresh token, and expiration time) will be saved back into the session automatically.
768775
This will in turn, update the `access_token`, `id_token` and `expires_at` fields of `tokenset` in the session.
769776

777+
### Mitigating Token Expiration Race Conditions in Latency-Sensitive Operations
778+
779+
For applications where an API call might be made very close to the token's expiration time, network latency can cause the token to expire before the API receives it. To prevent this race condition, you can implement a strategy to refresh the token proactively when it's within a certain buffer period of its expiration.
780+
781+
The general approach is as follows:
782+
1. Before making a sensitive API call, get the session and check the `expiresAt` timestamp from the `tokenSet`.
783+
2. Determine if the token is within your desired buffer period (e.g., 30-90 seconds) of expiring.
784+
3. If it is, force a token refresh by calling `auth0.getAccessToken({ refresh: true })`.
785+
4. Use the newly acquired access token for your API call.
786+
787+
This ensures that the token you send is guaranteed to be valid for at least the duration of the buffer, accounting for potential network delays.
788+
789+
> [!IMPORTANT]
790+
> This strategy is **not** a solution for long-running operations that take longer than the token's total validity period (e.g., 10 minutes). In those cases, the token will still expire mid-operation. The correct approach for long-running tasks is to call `getAccessToken()` immediately before the operation that requires it, ensuring you have a fresh token. The buffer is only for mitigating latency-related failures in short-lived requests.
791+
770792
## `<Auth0Provider />`
771793

772794
### Passing an initial user from the server
@@ -1457,4 +1479,5 @@ export async function middleware(request) {
14571479
14581480
### Run code after callback
14591481
Please refer to [onCallback](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#oncallback)
1460-
for details on how to run code after callback.
1482+
for details on how to run code after callback.
1483+
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The Auth0 Next.js SDK is a library for implementing user authentication in Next.
1010

1111
## Documentation
1212

13-
- [QuickStart](https://auth0.com/docs/quickstart/webapp/nextjs)- our guide for adding Auth0 to your Next.js app.
13+
- [QuickStart](https://auth0.com/docs/quickstart/webapp/nextjs) - our guide for adding Auth0 to your Next.js app.
1414
- [Examples](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md) - lots of examples for your different use cases.
1515
- [Security](https://github.com/auth0/nextjs-auth0/blob/main/SECURITY.md) - Some important security notices that you should check.
1616
- [Docs Site](https://auth0.com/docs) - explore our docs site and learn more about Auth0.

0 commit comments

Comments
 (0)