axios 401 unauthorized refresh token multipal request

Solutions on MaxInterview for axios 401 unauthorized refresh token multipal request by the best coders in the world

showing results for - "axios 401 unauthorized refresh token multipal request"
Kira
07 Apr 2017
1//https://www.npmjs.com/package/axios-auth-refresh
2
3//use this package it is working properly
4
5
6  const dispatch =  useDispatch();
7  
8  const refreshAuthLogic = failedRequest => RefreshToken().then(res => {
9    const response = res.data;
10    dispatch(auth.actions.RefreshTokenAction(response));
11      localStorage.setItem('authToken', response.accessToken);
12      localStorage.setItem('refreshToken', response.refreshToken);
13      failedRequest.response.config.headers['Authorization'] = 'Bearer ' + response.accessToken;
14      return Promise.resolve();
15  });
16
17// Instantiate the interceptor
18createAuthRefreshInterceptor(axios, refreshAuthLogic,1);
19
20
21
22//RefreshToken() this function will return the response frim Token refresh api call
23response_example = {
24  accessToken:'string',
25  refreshToken:'string
26};
27
Stefano
20 Jan 2018
1let refreshTokenPromise // this holds any in-progress token refresh requests
2
3// I just moved this logic into its own function
4const getRefreshToken = () => http.post('/token/refresh', {}, {
5  withCredentials: true,
6  headers: { 'X-CSRF-TOKEN': Vue.$cookies.get('csrf_refresh_token') }
7}).then(() => Vue.$cookies.get('csrf_access_token'))
8
9http.interceptors.response.use(r => r, error => {
10  if (error.config && error.response && error.response.status === 401) {
11    if (!refreshTokenPromise) { // check for an existing in-progress request
12      // if nothing is in-progress, start a new refresh token request
13      refreshTokenPromise = getRefreshToken().then(token => {
14        refreshTokenPromise = null // clear state
15        return token // resolve with the new token
16      })
17    }
18
19    return refreshTokenPromise.then(token => {
20      error.config.headers['X-CSRF-TOKEN'] = token
21      return http.request(error.config)
22    })
23  }
24  return Promise.reject(error)
25})
26