Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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)
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: dev console with colored font 
Javascript :: spring reactive web client throw exception test 
Javascript :: https://stackoverflow.com/questions/19380738/mongoose-nested-query-on-model-by-field-of-its-referenced-model 
Javascript :: serve public folder express without file extension 
Javascript :: state changes when changing route useContext next 
Javascript :: kubernetes get cluster 
Javascript :: cantsee auto complete for node jsmodules in vs code 
Javascript :: how to run the counter when we reach at a specific section in jquery 
Javascript :: string to number javascript & remove text 
Javascript :: javascript random function 
Javascript :: regular expressiong to indentify bible references in a sentence 
Javascript :: var quotes 
Javascript :: Raphael JS store arbitrary data 
Javascript :: how to make a <li when clicked on a button js 
Javascript :: how to update a state with an array react 
Javascript :: Arr::flatten() The Arr::flatten method flattens a multi-dimensional array into a single level array: 
Javascript :: how to add,delete,update property in javascript object 
Javascript :: node load testing-01 
Javascript :: how to disable gravity for an object in matter.js 
Javascript :: discord.js send dm to specific user 
Javascript :: babel 7 ReferenceError: regeneratorRuntime 
Javascript :: initializing a property asynchronously 2 
Javascript :: float vape pen instructions 
Javascript :: multi level route static file not found issue in express 
Javascript :: descomponer un numero js 
Javascript :: how to show name of inactive also in react-navigation-material-bottom-tabs 
Javascript :: how to put value in arrar 
Javascript :: como gerar numeros aleatorios em javascript a partir de uma função 
Javascript :: javascript const memory 
Javascript :: javascript random letters and numbers 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =