Registry / security / axios-token-manager

axios-token-manager

JSON →
library1.0.1jsnpmunverified

A library to cache and automatically refresh authentication tokens for Axios requests, with support for expiring tokens and recovery from early revocation. Version 1.0.1 is the latest. It adds request and response interceptors to an Axios instance, fetching and applying tokens, and retries failed requests with a new token on 401/403 errors. Differentiated by built-in recovery cycle and configurable callbacks, suitable for both Node.js and browser. Release cadence is low, with infrequent updates.

npm install axios-token-manager
INSTALL
IMPORT
SIG · AXIOS-TOKEN-MANAGE
A
axios-token-manager
securityjavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TokenManager
import { TokenManager } from 'axios-token-manager'
import TokenManager from 'axios-token-manager'
Named export, not default. Also works with Typescript.
TokenManager
const { TokenManager } = require('axios-token-manager')
const TokenManager = require('axios-token-manager')
CommonJS destructured import for named export.
TokenManagerConfig
import { TokenManagerConfig } from 'axios-token-manager'
Type export for configuration object, only TypeScript.

Initialize TokenManager with a fetchToken function and apply to an Axios instance to auto-manage authorization headers.

import axios from 'axios'; import { TokenManager } from 'axios-token-manager'; const fetchNewToken = async () => { const response = await axios.post('https://auth.example.com/token', { grant_type: 'client_credentials', client_id: process.env.CLIENT_ID ?? '', client_secret: process.env.CLIENT_SECRET ?? '' }); return { access_token: response.data.access_token, expires_in: response.data.expires_in }; }; const axiosInstance = axios.create({ baseURL: 'https://api.example.com' }); const tokenManager = new TokenManager({ fetchToken: fetchNewToken, axiosInstance }); // Now all requests via axiosInstance automatically include auth token axiosInstance.get('/data').then(response => console.log(response.data));
Debug
Known issues
breakingVersions <1.0.0 are beta and not stable; API may have changed significantly.
fix
Upgrade to version >=1.0.0 or latest.
affects: <1.0.0
deprecatedThe 'retryOn' configuration option in v0.x.x is deprecated in v1.0.0; use 'recovery' options instead.
fix
Migrate to new 'recovery' configuration as per migration guide.
affects: <1.0.0
gotchaTokenManager uses interceptors; multiple instances on the same Axios instance may conflict.
fix
Use separate Axios instances for different TokenManagers.
affects: >=1.0.0
gotchaThe 'fetchToken' function must return an object with 'access_token' and 'expires_in' fields; incorrect format causes errors.
fix
Ensure the returned object includes both fields as shown in the quickstart.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot read property 'access_token' of undefined
fetchToken function does not return the expected object with access_token field.
fix
Check the return structure of fetchToken; it must include { access_token: string, expires_in: number }.
TokenManager is not a constructor
Using default import instead of named import.
fix
Use import { TokenManager } from 'axios-token-manager' instead of import TokenManager from 'axios-token-manager'.
Axios instance is not provided
Missing axiosInstance in TokenManager configuration.
fix
Provide an Axios instance created with axios.create() as the axiosInstance option.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
axiosrequiredPeer dependency required to provide the Axios instance for interceptors
Agent activity
25 hits · last 30 days
node
24
Resources