question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Still have problems with multiple request

See original GitHub issue

Hi I have problem with my multiple requests. Only the first 401 error probably getting new access token, second not. 2020-09-21 18_03_42-Window

Here is my code. `import axios from “axios”; import createAuthRefreshInterceptor from “axios-auth-refresh”;

const baseUrl = “https://****.com/”; const instance = axios.create({ baseURL: baseUrl, });

// Function that will be called to refresh authorization const refreshAuthLogic = (failedRequest: any) => axios .post(“https://****/api/token/refresh/”, { refresh: localStorage.getItem(“refreshToken”), }) .then((tokenRefreshResponse) => { localStorage.removeItem(“accessToken”); localStorage.setItem(“accessToken”, tokenRefreshResponse.data.access);

  console.log(tokenRefreshResponse.data.access);

  failedRequest.response.config.headers["Authorization"] =
    "Bearer " + tokenRefreshResponse.data.access;

  return Promise.resolve();
});

createAuthRefreshInterceptor(instance, refreshAuthLogic);

export default instance;` No matter if I set the newest options like skipWhileRefreshing, pauseInstanceWhileRefreshing - situation is the same. I tried previous closed issues but didnt work 😦

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
Flyrellcommented, Sep 23, 2020

Well, it’s probably because you mix instance and axios like it’s the same thing. Try to use instance after it’s created and it will work just fine 🙂

So the final code would be:

import axios from "axios";
import createAuthRefreshInterceptor from "axios-auth-refresh";

const baseUrl = "https://***/";

const instance = axios.create({
  baseURL: baseUrl,
});

// Function that will be called to refresh authorization
const refreshAuthLogic = (failedRequest: any) =>
  instance
    .post("https://***/api/token/refresh/", {
      refresh: localStorage.getItem("refreshToken"),
    })
    .then((tokenRefreshResponse) => {
      localStorage.setItem("accessToken", tokenRefreshResponse.data.access);

      console.log(tokenRefreshResponse.data.access);

      failedRequest.response.config.headers["Authorization"] =
        "Bearer " + tokenRefreshResponse.data.access;
      return Promise.resolve();
    });

function getAccessToken() {
  return localStorage.getItem("accessToken");
}

// Use interceptor to inject the token to requests
instance.interceptors.request.use((request) => {
  request.headers["Authorization"] = `Bearer ${getAccessToken()}`;
  return request;
});

createAuthRefreshInterceptor(instance, refreshAuthLogic);

export default instance;
2reactions
lensanagcommented, Sep 23, 2020

You need put the logic for set auth token on an axios interceptor as the example on documentation:

axios.interceptors.request.use(request => {
    request.headers['Authorization'] = `Bearer ${getAccessToken()}`;
    return request;
});

previusly you define the function getAccessToken for example on localStorage.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with multiple requests and route / wait #3308 - GitHub
Current behavior: I am using the alias of cy. route() and on the page there are 2 requests for the same alias. For...
Read more >
Python Session not working for multiple requests
Problem : Using Requests, I am able to successfully make the api calls and get the work done. No problems using Requests except...
Read more >
oData – Insert issues with multiple requests - SAP Community
Hi,. In this example there are 2 functions, and accessing ES5 system. 1)Inserts Single Record into Product table onInsertSingleRecord() ...
Read more >
Handling Concurrent Requests in a RESTful API - Medium
The problem is very simple: users A and B requested a resource more or less at the same time, and got the same...
Read more >
The system has detected multiple requests affecting this data.
Problem. When a user is making changes on the QRadar User Interface and saves them, the following error message is displayed: "Invalid Request:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found