How to Refresh Tokens in Cognito using Amplify JS

If you are using Amazon Cognito via Amplify JS and if you need to refresh tokens, then all you need to do is following:

import { Auth } from 'aws-amplify';

Auth.currentSession()
  .then(data => console.log(data))
  .catch(err => console.log(err));

Above snippet is from the Amplify JS documentation.

Auth.currentSession() will return a CognitoUserSession object that contains JWT accessToken, idToken, and refreshToken.

This method will automatically refresh the accessToken and idToken if tokens are expired and a valid refreshToken is presented. So if you need to refresh the session, using this method is the easiest way to do it.

Comments