Skip to content

Commit bd34bb2

Browse files
added docs
1 parent 1dddd67 commit bd34bb2

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

EXAMPLES.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,69 @@ export async function middleware(request: NextRequest) {
520520
}
521521
```
522522

523+
### Forcing Access Token Refresh
524+
525+
In some scenarios, you might need to explicitly force the refresh of an access token, even if it hasn't expired yet. This can be useful if, for example, the user's permissions or scopes have changed and you need to ensure the application has the latest token reflecting these changes.
526+
527+
The `getAccessToken` method provides an option to force this refresh.
528+
529+
**App Router (Server Components, Route Handlers, Server Actions):**
530+
531+
When calling `getAccessToken` without request and response objects, you can pass an options object as the first argument. Set the `refresh` property to `true` to force a token refresh.
532+
533+
```typescript
534+
// app/api/my-api/route.ts
535+
import { getAccessToken } from '@auth0/nextjs-auth0';
536+
537+
export async function GET() {
538+
try {
539+
// Force a refresh of the access token
540+
const { token, expiresAt } = await getAccessToken({ refresh: true });
541+
542+
// Use the refreshed token
543+
// ...
544+
545+
return Response.json({ token, expiresAt });
546+
} catch (error) {
547+
console.error('Error getting access token:', error);
548+
return Response.json({ error: 'Failed to get access token' }, { status: 500 });
549+
}
550+
}
551+
```
552+
553+
**Pages Router (getServerSideProps, API Routes):**
554+
555+
When calling `getAccessToken` with request and response objects (from `getServerSideProps` context or an API route), the options object is passed as the third argument.
556+
557+
```typescript
558+
// pages/api/my-pages-api.ts
559+
import { getAccessToken, withApiAuthRequired } from '@auth0/nextjs-auth0';
560+
import type { NextApiRequest, NextApiResponse } from 'next';
561+
562+
export default withApiAuthRequired(async function handler(
563+
req: NextApiRequest,
564+
res: NextApiResponse
565+
) {
566+
try {
567+
// Force a refresh of the access token
568+
const { token, expiresAt } = await getAccessToken(req, res, {
569+
refresh: true
570+
});
571+
572+
// Use the refreshed token
573+
// ...
574+
575+
res.status(200).json({ token, expiresAt });
576+
} catch (error: any) {
577+
console.error('Error getting access token:', error);
578+
res.status(error.status || 500).json({ error: error.message });
579+
}
580+
});
581+
```
582+
583+
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.
584+
This will in turn, also update the `id_token` field of `tokenset` in the session.
585+
523586
## `<Auth0Provider />`
524587

525588
### Passing an initial user from the server

src/server/client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ export class Auth0Client {
322322
* NOTE: Server Components cannot set cookies. Calling `getAccessToken()` 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.
323323
* It is recommended to call `getAccessToken(req, res)` in the middleware if you need to retrieve the access token in a Server Component to ensure the updated token set is persisted.
324324
*/
325+
/**
326+
* @param options Optional configuration for getting the access token.
327+
* @param options.refresh Force a refresh of the access token.
328+
*/
325329
async getAccessToken(
326330
options?: GetAccessTokenOptions
327331
): Promise<{ token: string; expiresAt: number; scope?: string }>;
@@ -330,6 +334,11 @@ export class Auth0Client {
330334
* getAccessToken returns the access token.
331335
*
332336
* This method can be used in middleware and `getServerSideProps`, API routes in the **Pages Router**.
337+
*
338+
* @param req The request object.
339+
* @param res The response object.
340+
* @param options Optional configuration for getting the access token.
341+
* @param options.refresh Force a refresh of the access token.
333342
*/
334343
async getAccessToken(
335344
req: PagesRouterRequest | NextRequest,

0 commit comments

Comments
 (0)