Skip to content

Commit

Permalink
v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid Zbinski committed Nov 23, 2018
1 parent 17c2d6f commit b40cbc8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
27 changes: 27 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Contributing

I am open to, and grateful for, any contributions made by the community.
Because the life is too short to write a lot of documentation, I've copied some rules
from axios' [CONTRIBUTING](https://raw.githubusercontent.com/axios/axios/master/CONTRIBUTING.md) docs.

### Commit Messages

Commit messages should be verb based, using the following pattern:

- `Fixing ...`
- `Adding ...`
- `Updating ...`
- `Removing ...`

### Documentation

Please update the docs accordingly so that there are no discrepencies between the package and the documentation.

### Developing

Please, use [yarn](https://yarnpkg.com/en/docs/install) as a package manager if it's not a problem for you.
Please, always include changes to `dist/` in your pull request.
Please, do not include any `OS/IDE specific files` in your pull request.

### Build
Please, use `yarn build` to build the package.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,30 @@ createAuthRefreshInterceptor(axios, function refreshAuthLogic () {

#### Parameters
- `axios` - an instance of Axios used in project/call
- `refreshAuthLogic` - a Function used for refreshing authentication
- `refreshAuthLogic` - a Function used for refreshing authentication (**must return a promise**)

## Usage

In order to activate the interceptors, you need to import a function from `axios-auth-refresh`
which is *exported by default* and call it with the **axios instance** you want the interceptors for,
as well as the **refresh authorization function** where you need to write the logic for refreshing the authorization.

The interceptors will then be bind onto the axios instance and logic will be ran whenever [401 (Unauthorized)](https://httpstatuses.com/401) status
is returned from a server. All new requests created while the refreshAuthLogic is processed will be bind onto the
Promise returned from the refreshAuthLogic function.

```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();
});
}
// Function that will be called to refresh authorization
const refreshAuthLogic = () => 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);
createAuthRefreshInterceptor(axios, refreshAuthLogic);

// Make a call
axios.get('https://www.example.com/restricted/area')
Expand Down

0 comments on commit b40cbc8

Please sign in to comment.