Auth.currentSession() is returning expired JWT token in react native
I am trying to fetch JWT token every time a user opens app and store it in the local storage by overwriting it with old JWT token. But Auth.currentSession()
is returning the expired JWT token.
useEffect(() => {
try {
const fetchToken = async () => {
let authToken = await AsyncStorage.getItem("token");
console.log("authToken in hook:", authToken); // old token
if (authToken) {
authToken = await (await Auth.currentSession())
.getAccessToken()
.getJwtToken();
setToken(authToken);
const loggedInUserInfo = await Auth.currentUserInfo();
setUserInfo(loggedInUserInfo.attributes);
await AsyncStorage.setItem("token", authToken);
console.log("token resestted with new one", authToken); // expired/old token
setIsLoggedIn(true);
}
};
开发者_运维问答 fetchToken();
} catch (err) {
console.log(err);
}
}, []);
How can I handle this expiration of jwt token?
精彩评论