import axios, { AxiosResponse } from "axios"; import { API } from "./api/index"; import { AuthState } from "./auth/interfaces"; import { setToken } from "./auth/actions"; /** What to do when the Token refresh request completes. */ const ok = (x: AxiosResponse) => { setToken(x.data); // Start using new token in HTTP requests. return x.data; }; /** Grab a new token from the API (won't extend token's exp. date). * Redirect to home page on failure. */ export const maybeRefreshToken = (old: AuthState): Promise => { API.setBaseUrl(old.token.unencoded.iss); setToken(old); // Precaution: The Axios interceptors might not be set yet. return axios .get(API.current.tokensPath) .then(ok, () => Promise.resolve(undefined)); };