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
Copy file name to clipboardExpand all lines: EXAMPLES.md
+26-3Lines changed: 26 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -162,6 +162,10 @@ export default function Profile() {
162
162
163
163
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:
164
164
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
+
165
169
```tsx
166
170
import { auth0 } from"@/lib/auth0";
167
171
@@ -321,7 +325,7 @@ Then you can access your API from the frontend with a valid session cookie.
@@ -547,6 +551,9 @@ export async function middleware(request: NextRequest) {
547
551
548
552
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.
549
553
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
+
550
557
### In the browser
551
558
552
559
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(
767
774
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.
768
775
This will in turn, update the `access_token`, `id_token` and `expires_at` fields of `tokenset` in the session.
769
776
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
+
770
792
## `<Auth0Provider />`
771
793
772
794
### Passing an initial user from the server
@@ -1457,4 +1479,5 @@ export async function middleware(request) {
1457
1479
1458
1480
### Run code after callback
1459
1481
Please refer to [onCallback](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#oncallback)
0 commit comments