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

Appropriate way to determine whether UI will be triggered? #127

Open craigatproperly opened 6 years ago

craigatproperly commented 6 years ago

When I use getSession() it could trigger UI or could exchange refresh tokens. I am trying to implement a UI that prompts for "Login" if there is no unexpired token and instead shows "Signout" if there is.

I can use the success callback whether or not it exchanges refresh tokens or it does the login, but, basically I would like to be able to predict if getSession is going to trigger a redirect.

Is there a recommended/appropriate way to do that check? I feel like I want something like hasSession = "hasIDToken || (hasRefreshToken && refreshToken.exp < now()) " or equivalent.

dankantor commented 5 years ago

+1

I call getSession to find out if the user is logged in or not. If they are not logged in, I don't want to redirect them immediately to the hosted UI. Is there a way to tell it to not auto-redirect or a different method to see if they are logged in or not?

craigatproperly commented 5 years ago

I don't know that it is part of the API contract, but, I found this utility function that performed a check similar to what I wanted. So I am using this for now. It appears to be working, but, I haven't systematically checked all the expiry and logout scenarios: https://github.com/aws/amazon-cognito-auth-js/blob/1ea0f1d9282201f31411ac87b8a96a244793c1f4/es/CognitoAuth.js#L831

chittasec commented 5 years ago

How exactly to use this please ? I want to check if user has logged in.. But before that the UI gets triggred and errors out .