Created
December 11, 2020 21:00
-
-
Save sudomann/f6673e230311ed145a289dcc1aabcc39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tokenChecker = (config) => { | |
if (Transport.Http.isAccessTokenValid(config)) return config; | |
try { | |
Transport.Auth.fetchNewAccessTokenAsync().then((access) => { | |
/* Transport.Http.updateAuthorizationHeader accepts a token string | |
then formats it into a proper Authorization header string. | |
It then assigns that value to the axios instance's .defaults.headers.common.Authorization | |
for future requests. | |
Finally it returns that formatted header string for use by the caller. | |
In this case, I want to add it to the current request before it goes out | |
since its Authorization header is invalid. | |
*/ | |
config.headers.Authorization = Transport.Http.updateAuthorizationHeader( | |
access, | |
); | |
return Promise.resolve(config); | |
}); | |
} catch (e) { | |
switch (true) { | |
// handle various errors | |
} | |
} | |
return config; | |
}; | |
const successfulSignInSteps = (token) = { | |
// ... | |
Transport.Http.interceptors.request.use(tokenChecker); | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment