3

I have a lambda function which contains the following code;

    import {
      CognitoRefreshToken,
      CognitoAccessToken,
      CognitoIdToken,
      CognitoUserSession
    } from "amazon-cognito-identity-js";
........................
..........................
          const body = JSON.parse(event.body);
          const RefreshToken = new CognitoRefreshToken({
            RefreshToken: body.refreshToken
          });
          const IdToken = new CognitoIdToken({
            IdToken: body.idToken
          });
          const AccessToken = new CognitoAccessToken({
            AccessToken: body.accessToken
          });
          const sessionData = {
            IdToken: IdToken,
            AccessToken: AccessToken,
            RefreshToken: RefreshToken
          };
          const cachedSession = new CognitoUserSession(sessionData);
          if(cachedSession.isValid()) {
            //session is valid
          } else {
            //session expired(idToken and accessToken has expired)
          }

The problem I am facing is the check cachedSession.isValid() is always returning true even if the idToken and accessToken has expired. But from their official documentation it should return true only when the accessToken and idToken is valid.

What is wrong here. Any help would be very much appreciated.

For reference I followed this https://gist.github.com/kndt84/5be8e86a15468ed1c8fc3699429003ad for implementing the authorization flow.

BKM
  • 6,949
  • 7
  • 30
  • 45
  • 1
    I found the answer here. Hope it helps someone too https://stackoverflow.com/questions/50010309/validate-cognito-session-in-lambda-function – BKM Apr 18 '19 at 13:48
  • I followed the same gist and have the same problem. The solution is to use CognitoIdentityServiceProvider instead of CognitoUserSession for session/token vlaidation ? – wlarcheveque Oct 04 '19 at 20:24

0 Answers0