Skip to content

Instantly share code, notes, and snippets.

@sudomann
Created December 11, 2020 21:00
Show Gist options
  • Save sudomann/f6673e230311ed145a289dcc1aabcc39 to your computer and use it in GitHub Desktop.
Save sudomann/f6673e230311ed145a289dcc1aabcc39 to your computer and use it in GitHub Desktop.
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