amazon-archives / amazon-cognito-auth-js

The Amazon Cognito Auth SDK for JavaScript simplifies adding sign-up, sign-in with user profile functionality to web apps.
Apache License 2.0
424 stars 232 forks source link

How to handle token refresh? #175

Closed kocur4d closed 5 years ago

kocur4d commented 5 years ago

I am aware that there are few similar issues but they are left unanswered.

My use case is that I would like to check for a token valid on every graphql call I am making:

  const auth = getAuth()
  const user = auth.getCachedSession()
  if(!user.isValid())
    auth.refreshSession(auser.getRefreshToken().getToken())
  const session = auth.getSignInUserSession()
  const token = session.getIdToken().getJwtToken() 

But this doesn't work after an hour I am getting:

screen shot 2019-02-04 at 15 59 31

This call fails with 'Invalid JWT Token - TokenExpiredError: jwt expired' from my server and the token gets updated for a next call.

awaiting auth.refreshSession() doesn't do anything and checking the source indeed confirms the need for auth.userhandler onSuccess method.

How can you go around it? I would like to only check, if token is valid, and update it when making calls that are using them.

AntonioBusillo commented 4 years ago

Hello @kocur4d how did you handle the token refresh?