amazon-archives / amazon-cognito-identity-js

Amazon Cognito Identity SDK for JavaScript
Other
986 stars 452 forks source link

Cookie Storage not working #609

Closed woodpav closed 6 years ago

woodpav commented 6 years ago

Expected Behavior

After authenticating and refreshing the page, pool.getCurrentUser() should return the user instead of null.

Actual Behavior

User is successfully authenticated and tokens are retrieved. Storage is set in CognitoUserPool and CognitoUser on construction but calling pool.getCurrentUser() returns null.

Code

const DOMAIN = ".localhost:5000"

import { CognitoUserPool, CognitoUser, CookieStorage } from 'amazon-cognito-identity-js';

var pool = new CognitoUserPool({
  UserPoolId: COGNITO_USER_POOL_ID,
  ClientId: COGNITO_CLIENT_ID,
  Storage: new CookieStorage({ domain: DOMAIN })
});

var user = new CognitoUser({
  Username: username,
  Pool: pool,
  Storage: new CookieStorage({ domain: DOMAIN })
});

user.authenticateUser(auth, { ... });  // successful

pool.getCurrentUser();  // returns null after refresh

Maybe worth noting that cookies are blank. (However if I set cookies through js-cookies it works as expected.)

import * as Cookies from 'js-cookie';
Cookies.get()  // returns {}
woodpav commented 6 years ago

const DOMAIN = "localhost"

fahrenq commented 6 years ago

If you found this issue while trying to fix this 'bug' and @woodpav's answer not working - try to set secure property of CookieStorage to false.

new CookieStorage({ domain: 'localhost', secure: false })