AirLabsTeam / react-native-aws-cognito-js

React Native AWS Cognito JS SDK
Other
134 stars 19 forks source link

cognitoUser/userPool.storage.sync is not a function #8

Closed MattyK14 closed 7 years ago

MattyK14 commented 7 years ago

From the README.md I'm using:

userPool.storage.sync((err, result) => {
  // MemoryStorage is now updated with AsyncStorage values
});

or

cognitoUser.storage.sync((err, result) => {
  // MemoryStorage is now updated with AsyncStorage values
});

I played around with the example application without a crash, the result logs 'SUCCESS'.

Here's an example snippet of where I've tried to use it:

const authenticationData = {
      Username: username,
      Password: password,
    };

    var authenticationDetails = new AuthenticationDetails(authenticationData);

    var poolData = {
      UserPoolId: appConfig.UserPoolId,
      ClientId: appConfig.ClientId
    };

    var userPool = new CognitoUserPool(poolData);

    var userData = {
      Username: username,
      Pool: userPool
    };

    var cognitoUser = new CognitoUser(userData);

    cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: (result) => {
        console.log(`ID.JWT TOKEN =====> ${result.getIdToken().getJwtToken()}`);
        AsyncStorage.setItem('token', result.getIdToken().getJwtToken());
        AWS.config.credentials = new CognitoIdentityCredentials({
          IdentityPoolId: appConfig.IdentityPoolId,
          Logins: {
            [`cognito-idp.${appConfig.region}.amazonaws.com/${appConfig.UserPoolId}`]: result.getIdToken().getJwtToken()
          },
        }, {
          region: 'us-east-1'
        });

        Intercom.logEvent('success_login_user', { username });

        AWS.config.credentials.refresh((error) => {
          if (error) console.log(error);
        });

        cognitoUser.storage.sync((err, res) => {
          if (err) console.log(err);
          console.log(res);
        });

        Intercom.logEvent('success_login_user');
        dispatch({
          type: types.LOGIN_USER_SUCCESS,
          payload: result
        });
  ...
}
hszeto commented 7 years ago

are you running react-native-aws-cognito-js version 0.0.4 ?

MattyK14 commented 7 years ago

I was not. So I should be able to use this to not force the user to enter their credentials each time they open the app?

vvavepacket commented 7 years ago

@MattyK14 how were you able to read cognitoUser from MemoryStorage ?

I'm using your code snippet, but it returns null

export const checkIfLoggedIn = () => {
    return (dispatch) => {
        dispatch({ type: DO_NOTHING });
        var userPool = new CognitoUserPool(appConfig);
        var cognitoUser = userPool.getCurrentUser();
        console.log(cognitoUser);
        if (cognitoUser != null) {
            cognitoUser.getSession(function(err, session) {
                if (err) {
                    console.log(err);
                    return;
                }
                console.log('session validity: ' + session.isValid());

                // NOTE: getSession must be called to authenticate user before calling getUserAttributes
                cognitoUser.getUserAttributes(function(err, attributes) {
                    if (err) {
                        // Handle error
                    } else {
                        // Do something with attributes
                        console.log(attributes);
                    }
                });
                // in the future, you can insert here code to set token to access aws resources based on user access
                Actions.main({ type: 'reset' });
            });
        }
    }
};
MattyK14 commented 7 years ago

@vvavepacket I was, but it was super inconsistent and had to shelf it for now. Sometimes cognitoUser would come back null.