amazon-archives / amazon-cognito-identity-js

Amazon Cognito Identity SDK for JavaScript
Other
984 stars 454 forks source link

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

Open ffxsam opened 6 years ago

ffxsam commented 6 years ago

(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 commented 6 years ago

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.

itrestian commented 6 years ago

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.