Created
March 28, 2018 16:54
-
-
Save OtavioBraga/5e9c1caf0977adfc515c03030aeefdcb to your computer and use it in GitHub Desktop.
Axios interceptor to add Bearer header and jwt refresh validation
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
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