Customizing Token Management

Implement and provide a custom TokenStore class for token get, set and clear operations.

Default functionality

By default, PassageJS and the Passage Elements will store, lookup and clear auth tokens in both localstorage (opens in a new tab) and in a cookie (opens in a new tab) using a consistent key value.

Creating a custom token store

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

TokenStore methods

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.

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.

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();
    }
}
interface AuthResult {
  redirectUrl: string;
  authToken: string;
  refreshToken: string; // if refresh tokens are enabled
  refreshTokenExpiration: 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.

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.

import { TokenStore } from '@passageidentity/passage-js';
 
export class MyTokenStore extends TokenStore {
    getRefreshToken(authResult){
        localStorage.getItem('passage_rt);
        return Promise.resolve();
    }
};