Gathering detailed insights and metrics for axios-token-interceptor
Gathering detailed insights and metrics for axios-token-interceptor
Gathering detailed insights and metrics for axios-token-interceptor
Gathering detailed insights and metrics for axios-token-interceptor
axios-auth-refresh
Axios plugin which makes it very easy to automatically refresh the authorization tokens of your clients
@types/axios-token-interceptor
TypeScript definitions for axios-token-interceptor
axios-token-manager
A manager for Auth Tokens using Axios in a Node API Server or a JS Client
axios-token-interceptor-support
An interceptor which makes it easier to work with tokens in [axios](https://github.com/mzabriskie/axios).
An interceptor which makes it easier to work with tokens in axios.
npm install axios-token-interceptor
Typescript
Module System
Node Version
NPM Version
99.6
Supply Chain
99
Quality
74.8
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
5,362,581
Last Day
7,567
Last Week
73,257
Last Month
245,497
Last Year
2,370,142
MIT License
36 Stars
8 Commits
7 Forks
4 Watchers
14 Branches
2 Contributors
Updated on Feb 22, 2023
Minified
Minified + Gzipped
Latest Version
0.2.0
Package Id
axios-token-interceptor@0.2.0
Size
4.23 kB
NPM Version
6.11.3
Node Version
12.11.1
Published on
Oct 26, 2019
Cumulative downloads
Total Downloads
Last Day
40.5%
7,567
Compared to previous day
Last Week
7.1%
73,257
Compared to previous week
Last Month
26.8%
245,497
Compared to previous month
Last Year
78%
2,370,142
Compared to previous year
1
An interceptor which makes it easier to work with tokens in axios.
1const tokenProvider = require('axios-token-interceptor'); 2 3const instance = axios.create({ 4 baseURL: 'https://api.example.com' 5}); 6 7// Configure the provider with the necessary options. 8const options = { ... }; 9instance.interceptors.request.use(tokenProvider(options)); 10 11// When a call to an endpoint is made, a token will be provided as a header. 12instance.get('/foo')
There are different ways to provide a token. You can provide the token as a static value:
1instance.interceptors.request.use(tokenProvider({ 2 token: 'abc' 3})); 4 5// This will send the "Authorization: Bearer abc" header when making the call to the API endpoint. 6instance.get('/foo')
Instead of providing a static value you can also use a method to get the token:
1instance.interceptors.request.use(tokenProvider({ 2 getToken: () => localStorage.get('access_token') 3})); 4 5// This will send the "Authorization: Bearer ..." header when making the call to the API endpoint. 6instance.get('/foo')
And this method can also return a promise:
1instance.interceptors.request.use(tokenProvider({ 2 getToken: () => someMethod() 3 .then(response => response.access_token); 4})); 5 6// This will send the "Authorization: Bearer ..." header when making the call to the API endpoint. 7instance.get('/foo')
The following options allow you to set the header and the header value:
1instance.interceptors.request.use(tokenProvider({ 2 token: 'abc', 3 header: 'X-Api-Key', 4 headerFormatter: (token) => 'token/' + token, 5})); 6 7// This will send the "X-Api-Key: token/abc" header when making the call to the API endpoint. 8instance.get('/foo')
In cases where getting a token is an expensive operation (eg: exchanging a refresh token for an access token) you'll want to cache this work for as long as the token is valid.
The following example shows how we can cache tokens for 8 hours:
1const cache = tokenProvider.tokenCache( 2 getTokenFromAuthorizationServer().then(res => res.body.access_token), 3 { maxAge: ms('8h') } 4); 5 6instance.interceptors.request.use(tokenProvider({ 7 getToken: cache 8}));
Now it could also be that the token itself contains the expiration time (this is typically expires_in
you'll get from your Authorization Server). In that case you can also use this to configure the maximum age of the cache:
1const cache = tokenProvider.tokenCache( 2 () => getTokenFromAuthorizationServer().then(res => res.body), 3 { getMaxAge: (body) => body.expires_in * 1000 } 4); 5 6instance.interceptors.request.use(tokenProvider({ 7 getToken: cache, 8 headerFormatter: (body) => 'Bearer ' + body.access_token, 9}));
And the cache can also be reset:
1const cache = tokenProvider.tokenCache( 2 getTokenFromAuthorizationServer().then(res => res.body), 3 { getMaxAge: (res) => res.expires_in * 1000 } 4); 5 6cache.reset();
Note that
expires_in
coming from your authorization server is expressed in seconds, so you'll need to convert it to milliseconds when returning it to thegetMaxAge
function.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/7 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
38 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More