Skip to content

Instantly share code, notes, and snippets.

@OtavioBraga
Created March 28, 2018 16:54
Show Gist options
  • Save OtavioBraga/5e9c1caf0977adfc515c03030aeefdcb to your computer and use it in GitHub Desktop.
Save OtavioBraga/5e9c1caf0977adfc515c03030aeefdcb to your computer and use it in GitHub Desktop.
Axios interceptor to add Bearer header and jwt refresh validation
axios.interceptors.request.use(async (config) => {
if (
!config.url.endsWith('login') ||
!config.url.endsWith('refresh') ||
!config.url.endsWith('signup')
) {
const userTokenExpiration = new Date(await AsyncStorage.getItem('userTokenExpiration'));
const today = new Date();
if (today > userTokenExpiration) {
// refresh the token here
const userRefreshToken = await AsyncStorage.getItem('userRefreshToken');
} else {
const userToken = await AsyncStorage.getItem('userToken');
config.headers.Authorization = `Bearer ${userToken}`;
}
}
return config;
}, (error) => {
// I cand handle a request with errors here
return Promise.reject(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment