furaiev / amazon-cognito-identity-dart-2

Unofficial Amazon Cognito Identity Provider Dart SDK, to easily add user sign-up and sign-in to your mobile and web apps with AWS.
MIT License
187 stars 114 forks source link

refreshing ID token only after every 50 minutes or so because AWS tokens are valid for 60 minutes #176

Closed nyck33 closed 2 years ago

nyck33 commented 2 years ago

I am using your example code's UserService and store the tokens in local storage.
I am refactoring code that does this:

this.jwtToken = this._userService.getSession().getIdToken().getJwtToken();

on each request to AWS to put in the header.
But if the idToken is good for 60 minutes, I only want to call UserService.init() every 50 or 55 minutes or so to save time making requests (maybe multiple requests to different endpoints in a short span so don't want to call UserSerivce.init() each time) and to load pages faster on Flutter Web. Is it advisable to use a timer in Dart to do this?

furaiev commented 2 years ago

I wouldn't use a timer. I think you can decode the token before each request and check if it is still valid (valid - ok, not valid - refresh)