question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getSession in amazon-cognito-identity-js returning undefined sporadically

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pl-synthcommented, Jul 26, 2019

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.

0reactions
github-actions[bot]commented, Jun 12, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found