amazon-archives / aws-cognito-angular-quickstart

An Angular(v5)-based QuickStart single-page app utilizing Amazon Cognito, S3, and DynamoDB (Serverless architecture)
https://cognito.budilov.com
Apache License 2.0
689 stars 300 forks source link

Handled issue when current user returns wrong data #36

Closed rakeshtembhurne closed 7 years ago

rakeshtembhurne commented 7 years ago

Issue: CognitoUtil::getCurrentUser(); returns wrong or unauthenticated version of current user.

Issue Replication https://gist.github.com/rakeshtembhurne/5e2f02abe78588fad0f66bb3e6881d43

Above is the diff shows code working correctly. Replacing old getUserPool() and getCurrentUser() causes errors.

vbudilov commented 7 years ago

It doesn't look like this PR adds any value to the project. Closing unless proven otherwise.

rakeshtembhurne commented 7 years ago

Hi Vladimir,

Issue and its replication I was trying to update name of the current logged in user, for which I created a function in class CognitoUtil.

    updateCredentials(): void {
        var attributeList = [];
        var attribute = {
            Name : 'nickname',
            Value : 'Rakesh'
        };
        attribute = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(attribute);
        attributeList.push(attribute);

        var cognitoUser = this.getCurrentUser();
        cognitoUser.updateAttributes(attributeList, function(err, result) {
            if (err) {
                console.warn(err);
                return;
            }
            console.info('call result: ' + result);
        });

    }

This function fails with following error: screen shot 2017-02-09 at 11 35 00 am

Trying to figure out the issue, I found that this line might be causing this issue:

return new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(CognitoUtil._POOL_DATA);

This line is present in CognitoUtil::getUserPool() and called when try to get current user (CognitoUtil::getCurrentUser() function)

Gist file in my previous message contains solution I created, and worked for me. So in short the issue is calling getCurrentUser() function sends back unauthenticated user.