Skip to content

Commit

Permalink
Merge pull request #2 from Flyrell/init
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
Flyrell authored Nov 22, 2018
2 parents 6cbdab4 + 8c41bc2 commit 8b9ac9c
Show file tree
Hide file tree
Showing 7 changed files with 3,344 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
52 changes: 51 additions & 1 deletion README.md
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(/* ... */);
```

1 change: 1 addition & 0 deletions dist/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 25 additions & 3 deletions package.json
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"
}
}
27 changes: 27 additions & 0 deletions src/index.js
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;
14 changes: 14 additions & 0 deletions webpack.config.js
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')
}
};
Loading

0 comments on commit 8b9ac9c

Please sign in to comment.