axios-auth-refresh icon indicating copy to clipboard operation
axios-auth-refresh copied to clipboard

Retry of the original request fails only if GET request

Open GiovanniSlabs opened this issue 2 years ago • 4 comments

Hi, this is my configuration

const sendRequest = axios.create({
    baseURL: import.meta.env.VITE_SERVER_URL,
    withCredentials: true
})

const refreshAuthLogic = (failedRequest) => sendRequest.post('/admin/refresh').then(() => {
    setUserInfo(prev => ({
        ...prev,
        logged: true,
    }))
    return Promise.resolve()
}).catch(error => {
    //se non va a buon fine butto fuori l'utente
    setUserInfo({
        logged: false,
        info: {}
    })

    return Promise.reject(error);
});

// Instantiate the interceptor
createAuthRefreshInterceptor(sendRequest, refreshAuthLogic);

my tokens are all saved in cookies.

The problem is that if the original request is a POST request it works. But if the original request is a GET the retry request after the refresh request fails with this error:

DOMException: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'function(header, parser) {
header = normalizeHeader(header);
if (!header)
  return void 0;
const key = findKey(this, header);
if (key) {
  const value = this[key];
  if (!parser) {
    return value;
  }
  if (parser === true) {
    return parseTokens(value);
  }
  if (utils_default.isFunction(parser)) {
    return parser.call(this, value, key);
  }
  if (utils_default.isRegExp(parser)) {
    return parser.exec(value);
  }
  throw new TypeError("parser must be boolean|regexp|function");
}
}' is not a valid HTTP header field value.

I don't know if i miss something or there is a bug.

thank you.

GiovanniSlabs avatar Oct 12 '22 11:10 GiovanniSlabs

@GiovanniSlabs am stuck with the same error and still looking for solutions. Did you find out? let me know, please)

olksndrdevhub avatar Oct 14 '22 09:10 olksndrdevhub

Hi @olksndrdevhub, yes after many test i find out that this problem is due to a bug in the new version of axios > 1.0.0

The error is when axios try to set the header in retring the failed request

axios(request_options).then(() => {
    ...
}).catch(error => {
    const originalRequest = error.config;

    return axios(originalRequest) // here it messes up in setting the header correctly
});

So using v0.27.2 of axios solve the problem.

GiovanniSlabs avatar Oct 14 '22 09:10 GiovanniSlabs

Thanks for catching that @GiovanniSlabs

Flyrell avatar Oct 17 '22 23:10 Flyrell

Any solutions to fix this instead of downgrading axios version?

Jumail avatar Nov 07 '22 20:11 Jumail