getSession in amazon-cognito-identity-js returning undefined sporadically
See original GitHub issueDescribe the bug
We’re using cognitoUser.getSession
to check whether a user is logged in but we’ve noticed that it occasionally returns undefined
. The user then gets kicked out to our login screen but if we refresh the page without entering credentials they’re still logged in.
To Reproduce Steps to reproduce the behavior: We haven’t found a way to reproduce the issue as it happens randomly.
Expected behavior
Call cognitoUser.getSession
and it returns either null
or the session object if a user is signed in.
Desktop (please complete the following information):
- OS: OSX Mojave 10.14.5
- Browser: Chrome
- Version [e.g. 22]
Smartphone (please complete the following information):
- Device: iPhone XS Max
- OS: iOS 12
- Browser: Chrome
- Version: latest
Sample code
const isSessionValid = (cognitoUser) => {
return cognitoUser.getSession((err, session) => {
if (err) {
CustomError(err);
return false;
}
// Return whether the session is valid
if (session.isValid()) {
return true;
}
return false;
});
}
export const isAuthenticated = () => {
// Get the current user
const cognitoUser = userPool.getCurrentUser();
// Check there is an actual user object
if (!cognitoUser) return false;
// Get the current session
return isSessionValid(cognitoUser);
};
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
amazon-cognito-identity-js.CognitoUser.getSession ... - Tabnine
Gets a new Cognito session. Returns a promise. const getCognitoSession = () => { return new Promise((resolve, reject) => { const cognitoUser ......
Read more >How to get session callback info? AWS - Cognito - JavaScript
I am trying to get the AccessKeyID and the SecretKey (ultimately to programatically input into a CRUD operation on dynamoDB with fine grained ......
Read more >AWS Cognito - Higher Order Component - Martin Tierney
A Higher Order Component is like a higher order function in Javascript. It is a function that takes a component and returns a...
Read more >amazon-cognito-identity-js - npm
The Amazon Cognito Identity SDK for JavaScript allows JavaScript enabled applications to sign-up users, authenticate users, view, delete, ...
Read more >Find Answers to AWS Questions about Amazon Cognito
Hi, We've setup the migration lambda correctly and it is exiting fine after finding and confirming the user. The memory allocated to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
amazon-cognito-identity-js => v3.0.14
Getting same thing, am able to reproduce it on our app. getSession is returning undefined. This happens only on very first load, once every 30 min. Reloading helps. Workaround is to call it again, not ideal ofc! There’re no errors or anything, just undefined.
This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server
*-help
channels or Discussions for those types of questions.