axios-refresh

1.0.2 • Public • Published

axios-refresh

Installing

Package manager

Using npm:

$ npm install axios-refresh

Once the package is installed, you can import the library using import approach:

import getAxiosRefreshInstance from "axios-refresh"

Features

  • refreshes access token on server 401 Unauthorized response
  • queues failed requests and automatically repeats them on refresh token success

Example

Note JS usage

import getAxiosRefreshInstance from "axios-refresh"

const refreshToken = ()=>{
  return axios.post(...).then(data => {
    setTokens()
    return data;
  });
}

// function on refresh token error (change location, remove tokens...) 
const onError = ()=>{...};


export const http = getAxiosRefreshInstance(refreshToken, onError);

http.interceptors.request.use((req: AxiosRequestConfig) => {
    if (req.headers) req.headers.Authorization = `Bearer ${...}`;
    else req.headers = {Authorization: `Bearer ${...}`};
    return req;
});


// Than for protected routes use http axios instance

// example of protected route
const fetchUsers = () => {
  return http.get(...);
}

Package Sidebar

Install

npm i axios-refresh

Weekly Downloads

3

Version

1.0.2

License

ISC

Unpacked Size

4.08 kB

Total Files

3

Last publish

Collaborators

  • valentinobestak