amazon-cognito-identity-js icon indicating copy to clipboard operation
amazon-cognito-identity-js copied to clipboard

How to tell if ID & access tokens are expired? How do we refresh?

Open ffxsam opened this issue 6 years ago • 2 comments

(side note: I hope someone is working on the documentation for this repo.. a bunch of use cases in the README doesn't cut it)

I have an app where a user might be using it for well over an hour. During that time, the ID and access tokens expire, and errors are thrown when trying to access AWS services that expect the user to be authorized via Cognito.

How/when do we properly detect expiration? And how do we refresh those tokens seamlessly so the user doesn't experience any interruptions?

ffxsam avatar Feb 10 '18 23:02 ffxsam

Aha. cognitoUser.refreshSession() is not documented.

https://github.com/aws/amazon-cognito-identity-js/blob/6059c168270f8cf6b5d786e0b9dddd74fb06aa62/src/CognitoUser.js#L1074-L1113

This is hacky, but I set up a 30-min timer to auto refresh the ID & access tokens. If there's a better way, I'm all ears.

ffxsam avatar Feb 10 '18 23:02 ffxsam

You can basically wrap your calls in a call to getSession in which case the SDK always checks if the access token is valid and if not it uses the refresh token to retrieve a new access token.

itrestian avatar Feb 20 '18 20:02 itrestian