Answers for "axios 401 unauthorized refresh token multipal request"

0

axios 401 unauthorized refresh token multipal request

let refreshTokenPromise // this holds any in-progress token refresh requests

// I just moved this logic into its own function
const getRefreshToken = () => http.post('/token/refresh', {}, {
  withCredentials: true,
  headers: { 'X-CSRF-TOKEN': Vue.$cookies.get('csrf_refresh_token') }
}).then(() => Vue.$cookies.get('csrf_access_token'))

http.interceptors.response.use(r => r, error => {
  if (error.config && error.response && error.response.status === 401) {
    if (!refreshTokenPromise) { // check for an existing in-progress request
      // if nothing is in-progress, start a new refresh token request
      refreshTokenPromise = getRefreshToken().then(token => {
        refreshTokenPromise = null // clear state
        return token // resolve with the new token
      })
    }

    return refreshTokenPromise.then(token => {
      error.config.headers['X-CSRF-TOKEN'] = token
      return http.request(error.config)
    })
  }
  return Promise.reject(error)
})
Posted by: Guest on October-04-2021
0

axios 401 unauthorized refresh token multipal request

//https://www.npmjs.com/package/axios-auth-refresh

//use this package it is working properly


  const dispatch =  useDispatch();
  
  const refreshAuthLogic = failedRequest => RefreshToken().then(res => {
    const response = res.data;
    dispatch(auth.actions.RefreshTokenAction(response));
      localStorage.setItem('authToken', response.accessToken);
      localStorage.setItem('refreshToken', response.refreshToken);
      failedRequest.response.config.headers['Authorization'] = 'Bearer ' + response.accessToken;
      return Promise.resolve();
  });

// Instantiate the interceptor
createAuthRefreshInterceptor(axios, refreshAuthLogic,1);



//RefreshToken() this function will return the response frim Token refresh api call
response_example = {
  accessToken:'string',
  refreshToken:'string
};
Posted by: Guest on October-04-2021

Code answers related to "axios 401 unauthorized refresh token multipal request"

Browse Popular Code Answers by Language