TokenManager
TheTokenManager class provides a simple, built-in solution for managing OAuth tokens with automatic expiration tracking.
Overview
The TokenManager handles:- Storing access tokens, refresh tokens, and ID tokens
- Tracking token expiration with a 60-second buffer
- Automatic expiration checking
- Token lifecycle management
The TokenManager stores tokens in memory. For production use with persistent
storage, implement custom token storage using the SDK’s callbacks
(
onTokensReceived, onTokensRefreshed, onTokensRevoked).Usage
Basic Setup
Complete Flow
Properties
accessToken
The current access token.
null if no token is stored.idToken
The current OpenID Connect ID token.
null if not available.refreshToken
The current refresh token.
null if not available.tokenType
The token type. Always
'Bearer' for OAuth 2.1.expiresAt
Timestamp (in milliseconds) when the access token expires. Includes a
60-second buffer.
null if no token is stored.scopes
Space-separated list of granted scopes.
null if no token is stored.Methods
setTokens()
Store tokens from an OAuth response.OAuth token response object
void
- Stores access token, refresh token, and ID token
- Calculates expiration time with a 60-second buffer
- Stores token type and scopes
isExpired()
Check if the access token is expired. Returns:boolean - true if expired or no token, false if valid
- Returns
trueif no token is stored - Returns
trueif no expiration time is set - Returns
trueif current time >= expiration time - Returns
falseif token is still valid
The expiration check includes a 60-second buffer to prevent using tokens that
are about to expire.
getAccessToken()
Get the current access token. Throws an error if the token is expired. Returns:string - The access token
Throws: Error if token is expired
isExpired() before calling getAccessToken() to avoid exceptions:
clear()
Clear all stored tokens and reset the manager. Returns:void
accessTokenidTokenrefreshTokenexpiresAtscopes
Complete Examples
Example 1: Basic Usage
Example 2: Automatic Token Refresh
Example 3: Token Lifecycle Management
Example 4: Monitoring Token Expiration
When to Use TokenManager vs Custom Storage
Use TokenManager when:
- Building a simple CLI tool or script
- Prototyping or testing
- Single-user, single-session applications
- Short-lived processes
Use Custom Storage when:
- Building production web applications
- Supporting multiple users
- Need persistent storage across sessions
- Require encrypted token storage
- Need distributed session management
Example: Custom Storage Implementation
Best Practices
Always check expiration before using tokens
Always check expiration before using tokens
Handle missing refresh tokens
Handle missing refresh tokens
Use try-catch with getAccessToken()
Use try-catch with getAccessToken()
Clear tokens on logout
Clear tokens on logout
Next Steps
AgentSDK Methods
Learn about all SDK methods
Utilities
PKCE, JWT parsing, and helper utilities
Configuration
SDK configuration options
Examples
See complete examples