Installations
npm install axios-token-interceptor
Releases
Unable to fetch releases
Developer
sandrinodimattia
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
12.11.1
NPM Version
6.11.3
Statistics
36 Stars
8 Commits
7 Forks
5 Watching
14 Branches
2 Contributors
Updated on 22 Feb 2023
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
4,016,209
Last day
5.2%
8,054
Compared to previous day
Last week
10.8%
43,027
Compared to previous week
Last month
3.4%
175,304
Compared to previous month
Last year
99.1%
1,833,270
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Axios Token Interceptor
An interceptor which makes it easier to work with tokens in axios.
Usage
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')
Providing a token
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')
Customizing the Header
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')
Caching
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
35 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-4w2v-q235-vp99
- Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.9
/10
Last Scanned on 2024-11-18
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