-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Flyrell/init
v1.0.1
- Loading branch information
Showing
7 changed files
with
3,344 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,52 @@ | ||
# axios-auth-refresh | ||
Axios plugin which makes it very easy to automatically refresh the authorization tokens of your clients | ||
Axios plugin that makes it easy to implement automatic refresh of authorization | ||
via axios' [interceptors](https://github.com/axios/axios#interceptors). | ||
|
||
Size: 1.25 KiB minified, 1.02 KiB gzipped | ||
|
||
|
||
## Installation | ||
|
||
Using [npm](https://www.npmjs.com/get-npm) or [yarn](https://yarnpkg.com/en/docs/install) | ||
|
||
```bash | ||
npm install axios-auth-refresh | ||
# or | ||
yarn add axios-auth-refresh | ||
``` | ||
|
||
## Syntax | ||
|
||
```javascript | ||
createAuthRefreshInterceptor(axios, function refreshAuthLogic () { | ||
// Return a Promise | ||
}); | ||
``` | ||
|
||
#### Parameters | ||
- `axios` - an instance of Axios used in project/call | ||
- `refreshAuthLogic` - a Function used for refreshing authentication | ||
|
||
## Usage | ||
|
||
```javascript | ||
import axios from 'axios'; | ||
import createAuthRefreshInterceptor from 'axios-auth-refresh'; | ||
|
||
// Function that will be called to refresh authentication | ||
function refreshAuth () { | ||
return axios.post('https://www.example.com/auth/token/refresh').then(res => { | ||
localStorage.setItem('token', res.data.token); | ||
return Promise.resolve(); | ||
}); | ||
} | ||
|
||
// Instantiate the interceptor (you can chain it as it returns the axios instance) | ||
createAuthRefreshInterceptor(axios, refreshAuth); | ||
|
||
// Make a call | ||
axios.get('https://www.example.com/restricted/area') | ||
.then(/* ... */) | ||
.catch(/* ... */); | ||
``` | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
{ | ||
"name": "axios-auth-refresh", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Axios plugin which makes it very easy to automatically refresh the authorization tokens of your clients", | ||
"main": "index.js", | ||
"keywords": [ | ||
"axios", | ||
"authentication", | ||
"authorization", | ||
"refresh token", | ||
"access token", | ||
"interceptor", | ||
"auto refresh" | ||
], | ||
"main": "src/index.js", | ||
"repository": "https://github.com/Flyrell/axios-auth-refresh", | ||
"author": "Dawid Zbiński <[email protected]>", | ||
"license": "MIT", | ||
"private": false | ||
"private": false, | ||
"scripts": { | ||
"build": "webpack" | ||
}, | ||
"dependencies": { | ||
"axios": "^0.18.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.1.6", | ||
"@babel/preset-env": "^7.1.6", | ||
"babel-loader": "^8.0.4", | ||
"webpack": "^4.26.0", | ||
"webpack-cli": "^3.1.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Creates an authentication refresh interceptor that binds to any error response. | ||
* If the response code is 401, interceptor tries to call the refreshTokenCall which must return a Promise. | ||
* After Promise is resolved/rejected the interceptor is revoked. | ||
* @param {Axios|Function|Object} axios - axios instance | ||
* @param {Function} refreshTokenCall - refresh token call which must return a Promise | ||
* @return {Axios} | ||
*/ | ||
function createAuthRefreshInterceptor (axios, refreshTokenCall) { | ||
const id = axios.interceptors.response.use(response => response, error => { | ||
|
||
// Reject promise if the error status is not 401 (Unauthorized) | ||
if (error.response && error.response.status !== 401) return Promise.reject(error); | ||
|
||
/* | ||
* When response code is 401 (Unauthorized), try to refresh the token. | ||
* Eject the interceptor so it doesn't loop in case token refresh causes the 401 response too. | ||
*/ | ||
axios.interceptors.response.eject(id); | ||
return refreshTokenCall() | ||
.then(() => axios(error.response.config)) | ||
.catch(error => Promise.reject(error)) | ||
.finally(() => createAuthRefreshInterceptor(axios, refreshTokenCall)); | ||
}); | ||
return axios; | ||
} | ||
export default createAuthRefreshInterceptor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" } | ||
] | ||
}, | ||
entry: './src/index.js', | ||
output: { | ||
filename: 'index.min.js', | ||
path: path.resolve(__dirname, 'dist') | ||
} | ||
}; |
Oops, something went wrong.