In my react project I am using AWS Cognito user pool for user management, for user authentication, I am using AWS Cognito idToken. after 90min the session will expire, then
Calling Auth.currentSession() should solve your problem. Amplify-js abstracts the refresh logic away from you.
Under the hood currentSession() gets the CognitoUser object, and invokes its class method called getSession(). It's this method, that does the following:
idToken, accessToken, refreshToken, and clockDrift from your storage.refreshSession() method of the CognitoUser class, which communicates to the AWS Identity Provider to generate a new set of tokens.All you have to do now is either:
Auth.currentSession() at regular intervalsAuth.currentSession() to get your token for each http request that you make.You could use a wrapper like this:
const getAccessJwtToken = async () => {
// Auth.currentSession() checks if token is expired and refreshes with Cognito if needed automatically
const session = await Auth.currentSession();
return session.getAccessToken().getJwtToken();
};
Lastly, this github discussion also introduces a very good manual way to refresh your token and introduces a use case for when you should explore that option.