getAccessToken function retrieves an access token for the currently authenticated user from client components. It automatically handles token refresh if the token has expired.
Import
This function can only be called from client components or browser environments.
Signature
Parameters
AccessTokenOptions
Configuration options for fetching the access token.
string
The unique identifier of the target API. Required when calling multiple APIs with different audiences.
When using
audience or scope, ensure Multi-Resource Refresh Tokens (MRRT) is configured in your Auth0 Application settings.string
Additional scopes to request beyond those granted during login.Example:
'read:profile write:profile'boolean
default:"false"
When
true, returns the full response object instead of just the access token string.Return Value
Default Return (string)
By default, returns the access token as a string:Full Response Return
WhenincludeFullResponse: true, returns an object with:
string
The access token string.
string
The scopes granted for this token.
number
Unix timestamp (in seconds) when the token expires.
number
Number of seconds until the token expires.
string
Token type (typically
"Bearer").Basic Usage
app/components/api-caller.tsx
Full Response Usage
Multi-Resource Refresh Tokens (MRRT)
When your application calls multiple APIs with different audiences, you must specify theaudience parameter:
Custom Scopes
Error Handling
Token Caching
For client-side token caching to avoid redundant requests:API Route
The client-sidegetAccessToken function makes a request to /auth/access-token (configurable via NEXT_PUBLIC_ACCESS_TOKEN_ROUTE).
The SDK automatically provides this route handler, but you can customize it:
app/api/auth/access-token/route.ts
TypeScript
Custom Environment Variables
.env.local
See Also
- getAccessToken (Server) - Server-side access token retrieval
- Multi-Resource Refresh Tokens - Auth0 MRRT documentation
- useUser - Access user profile data