Determine whether the token expires before the axios request. If it expires, request the token before sending the request.

1. When you encounter a problem with the expiration of token, enter the system to determine whether there is a token,. If not, request token first.

2. Determine whether the token expires before other subsequent requests are sent, and if so, re-request the token, and then send the request.

3. Dear gods, is there a good plan to intercept with axios? please answer, thank you!

Mar.11,2021

this is undoubtedly the best choice to use fly.js, which is basically the same as axios. Fly.js can check token, for asynchronous tasks in the request interceptor. For more information, please see fly.js

.
var csrfToken="";
var tokenFly=new Fly();
var fly=new Fly();
fly.interceptors.request.use(function (request) {
  log(`:path:${request.url}baseURL:${request.baseURL}`)
  if (!csrfToken) {
    log("tokentoken...");
    //
    fly.lock();
    return newFly.get("/token").then((d) => {
      request.headers["csrfToken"] = csrfToken = d.data.data.token;
      log("token: " + d.data.data.token);
      log(`:path:${request.url}baseURL:${request.baseURL}`)
      return request; //request
    }).finally(()=>{
      fly.unlock();//
    })
  } else {
    request.headers["csrfToken"] = csrfToken;
  }
})
Menu