Comment on page
Customizing Token Management
Implement and provide a custom TokenStore class for token get, set and clear operations.
By default, PassageJS and the Passage Elements will store, lookup and clear auth tokens in both localstorage and in a cookie using a consistent key value.
If you'd like to handle client-side auth token storage differently than the built-in default, you can pass your own TokenStore as a config option for either PassageJS or the Passage elements.
- Store and retrieve your tokens in the method best for your use case
- Perform custom logic or call an API before you set or remove a token
All class methods should return a promise.
Required Methods
getAuthToken
Returns a promise containing the auth token value for PassageJS to use for authorized API calls.
Example
import { TokenStore } from '@passageidentity/passage-js';
export class MyTokenStore extends TokenStore {
getAuthToken() {
const jwt = localStorage.getItem('passage_jwt')
return Promise.resolve(JSON.parse(jwt));
}
};
setTokens
Given an auth response on login completion you have the ability to set your token(s) via any storage mechanism (asynchronous or synchronous). It should return a promise.
Example
Parameters
import { TokenStore } from '@passageidentity/passage-js';
export class MyTokenStore extends TokenStore {
setTokens(authResult){
const passageToken = authResult.auth_token;
const passageRefreshToken = authResult.refresh_token;
localStorage.setItem('passage_jwt', passageToken);
localStorage.setItem('passage_rt', passageRefreshToken);
return Promise.resolve();
}
};
authResult
interface AuthResult {
redirect_url: string;
auth_token: string;
refresh_token: string; // if refresh tokens are enabled
refresh_token_expiration: number; // if refresh tokens are enabled
}
Optional Methods
clearTokens
This method is called when you use the Session
.signout()
method. It should clear your targeted tokens and return a promise. Example
import { TokenStore } from '@passageidentity/passage-js';
export class MyTokenStore extends TokenStore {
clearTokens(authResult){
localStorage.removeItem('passage_jwt);
localStorage.removeItem('passage_rt');
return Promise.resolve();
}
};
getRefreshToken
Returns a promise containing the refresh token value for PassageJS to use. This will be used silently behind the scenes when making authenticated requests. See Refresh Tokens for more details.
Example
import { TokenStore } from '@passageidentity/passage-js';
export class MyTokenStore extends TokenStore {
getRefreshToken(authResult){
localStorage.getItem('passage_rt);
return Promise.resolve();
}
};
Last modified 2mo ago