var authExpirationHandler, errorHandler; function catchUnauthorizedResponses(data) { if (data.status === 401 && !maybeLocalStorage.getItem("token")) { // ensure the in-memory session really is expired before destroying it return axios.get("/api/auth/session").then( (response) => { // we ARE still auth'd, so just throw the error down the chain throw data; }, (err) => { // local session is invalid, so invoke handler and throw the original error authExpirationHandler ? authExpirationHandler.call(null) : null; // TODO: remove self from interceptors chain throw data; }); } else { throw data; } } function authHeader(email, token) { return "Bearer email=\"" + email + "\", token=\"" + token + "\""; } function authorizeAxiosRequests(config) { if (session) { config.headers.Authorization = authHeader(email, token); } return config; } function catchAllErrorResponses(data) { // maybe don't call errorHandler if (!!authExpirationHandler && data.status === 401) ?? i dunno. errorHandler.call(null, data.status); } module.exports = { addErrorInterceptors (handler) { errorHandler = handler; // TODO: add catchAllErrorResponses to the interceptors chain }, addAuthInterceptors (email, token, handler) { authExpirationHandler = handler; // TODO: add catchUnauthorizedResponses to the interceptors chain // TODO: add authorizeAxiosRequests to the interceptors chain } }