1.0.2 • Published 1 year ago

axios-refresh v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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(...);
}