amazon-archives / amazon-cognito-auth-js

The Amazon Cognito Auth SDK for JavaScript simplifies adding sign-up, sign-in with user profile functionality to web apps.
Apache License 2.0
424 stars 232 forks source link

LocalStorage doesn't work #165

Closed YoniH closed 5 years ago

YoniH commented 5 years ago

When I set Storage: 'LocalStorage', my React page crashes on startup. The error is:

TypeError: this.storage.getItem is not a function CognitoAuth.getLastUser node_modules/amazon-cognito-auth-js/es/CognitoAuth.js:441 438 | CognitoAuth.prototype.getLastUser = function getLastUser() { 439 | var keyPrefix = 'CognitoIdentityServiceProvider.' + this.getClientId(); 440 | var lastUserKey = keyPrefix + '.LastAuthUser';

441 | var lastUserName = this.storage.getItem(lastUserKey); | ^ 442 | 443 | if (lastUserName) { 444 | return lastUserName;

My code:

import React, { Component } from 'react';
import { CognitoAuth } from 'amazon-cognito-auth-js';

const authData = {
    ClientId: '<my client id>',
    AppWebDomain: '<my domain>',
    TokenScopesArray: ['profile','openid'],
    RedirectUriSignIn: 'http://localhost:3000',
    RedirectUriSignOut: 'http://localhost:3000',
        UserPoolId: '<my pool id>',
        Storage: 'LocalStorage'
};

class App extends Component {
  constructor() {
    super();

    this.auth = new CognitoAuth(authData);

    this.handleSignIn = this.handleSignIn.bind(this);
    this.handleSignOut = this.handleSignOut.bind(this);
    this.test = this.test.bind(this);
  }

  handleSignIn() {
    this.auth.getSession();
  }

  handleSignOut() {
    this.auth.signOut();
  }

  test() {
    console.log(this.auth.isUserSignedIn());
    console.log(this.auth.getCurrentUser());
    console.log(this.auth.getSignInUserSession());
    console.log(this.auth.getUserContextData());
  }

  render() {
    return (
      <div>
        <button onClick={this.handleSignIn}>SignIn</button>
        <button onClick={this.handleSignOut}>SignOut</button>
        <button onClick={this.test}>Test</button>
      </div>
    );
  }
}

export default App;
chamathsilva commented 5 years ago

Try it without specifying the storage type. by default, it will use localStorage if localStorage not available it will automatically use in memory storage. (if you need to understand how it works look at the implemntation of StorageHelper.js )

i. e remove this line Storage: 'LocalStorage'

YoniH commented 5 years ago

Tried that already. When I do that, I see nothing in the local storage, even after I am redirected back to my page with id token and access token in the url. That's what made me explicitly specify LocalStorage in the first place

On Sat, 27 Oct 2018, 22:45 Chamath Silva notifications@github.com wrote:

Try it without specifying the storage type. by default, it will use localStorage if localStorage not available it will automatically use in memory storage. (if you need to understand how it works look at the implemntation of StorageHelper.js https://github.com/aws/amazon-cognito-auth-js/blob/master/src/StorageHelper.js )

i. e remove this line Storage: 'LocalStorage'

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws/amazon-cognito-auth-js/issues/165#issuecomment-433649908, or mute the thread https://github.com/notifications/unsubscribe-auth/AHVnyLRrLHphL8U73yUspXCa4sY5ZSikks5upLfugaJpZM4X6rwW .

YoniH commented 5 years ago

It's ok now, there was something else I missed. Thanks

NarendraChouhan0021 commented 4 years ago

@YoniH how did you solve this problem, i am facing problem. thank you in advance