* Which Category is your question related to? *
Auth, Storage
* What AWS Services are you utilizing? *
Cognito (User Pool), Hosted UI
* Provide additional details e.g. code snippets *
I am authenticating users using the Hosted UI in my React application. I have a Saga running as soon as my application starts which makes a call to Auth.currentSession() to determine whether or not the user is signed in.
export function* cognitoSaga() {
Amplify.configure({ Auth: userpool }); // configure user pool settings
let session;
try {
session = yield Auth.currentSession(); // check for session in local storage
} catch (err) {
console.error(err);
yield Auth.federatedSignIn(); // redirect to sign in page on hosted ui
return;
}
yield put(handleCognitoSigninAction({ session })); // proceed with application
}
The issue I'm seeing is that there seems to be a race condition when getting/setting the session from local storage. When the browser is redirected back to my application after signing in for the first time, yield Auth.currentSession() throws an error (No current user) which causes my application to redirect back to the Sign In page.
The only way I can think of to get around this issue is to add a delay right before looking in local storage to give the browser some time to populate the session, like the following.
let session;
try {
console.log(localStorage); // no tokens yet
yield delay(2000);
console.log(localStorage); // tokens are ready
session = yield Auth.currentSession(); // check for session in local storage
} catch (err) {
console.error(err);
yield Auth.federatedSignIn(); // redirect to sign in page on hosted ui
return;
}
Is this the correct way of using currentSession()? Is there some kind of flag I can use to determine if the tokens in storage are ready?
Thanks.
@ctlai95
I merged a fix for this, currently is published to unstable tag on npm if you can try out. We will publish to latest soon.
Most helpful comment
@ctlai95
I merged a fix for this, currently is published to
unstabletag on npm if you can try out. We will publish tolatestsoon.