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

No callback for getSession, how to deal with refresh? #114

Open raf202 opened 6 years ago

raf202 commented 6 years ago

I've been looking into the token refresh flow, what I would do is always call getSession with a callback to my API calls, that would make sure that my token is always valid. But I noticed in the code that getSession does not provide any callback functions.

How is the workflow to approach refresh without a callback? Setting up a timer to auto refresh seems a bit hacky to me.

erwinkarim commented 6 years ago

await and promises AFAIK doesn't work as getSession will complete immediately even when it takes 2-3 seconds for the token to be refreshed.

aswin1689 commented 6 years ago

I had the same issue. Instead of calling getSession, I checked if session is not valid and we have a refresh token, then call the https://{{appDomain}}/oauth2/token api directly with these parameters header: 'Content-Type': 'application/x-www-form-urlencoded' body: 'grant_type=refresh_token&client_id=${clientId}&refresh_token=${refreshToken}' using the Fetch API. In the then block, I passed the result to onSuccessExchangeForToken method found in this SDK.

jollsker commented 6 years ago

userhandler onSuccess or onFailure is called for getSession.