get refresh token in axios interceptor

14 Views Asked by At

I use axios interceptor response so I can get the refreshToken every time the accessToken expires but the problem here is that it doesn't seem to work

httpRequest.interceptors.response.use(
        (res) => res,
        async (error) => {
            const status = error.response ? error.response.status : null;
            if (status === 401 || status === 403) {

                await httpRequest
                    .get('auth/refresh', {
                        withCredentials: true,
                    })
                    .then(res => {
                        error.config.headers["Authorization"] = "Bearer " + res.data.accessToken;
                        localStorage.setItem("token", res.data.accessToken)
                    })
                    .catch((refreshTokenAPIError) => {
                        localStorage.removeItem('token');
                        return Promise.reject(refreshTokenAPIError);
                    });
                return httpRequest(error.config)
            }
            return Promise.reject(error);
        }
    )
0

There are 0 best solutions below