rastafalow
rastafalow

Reputation: 167

Cognito AmazonWebService authentication issue

I'm doing an user management with the AmazonWebService cognito and I having some difficulties to authenticate me to my user pool.

Do am I logged in if I just do:

login: function(username, password, _poolData) {
            var deferred = $q.defer();
            var authenticationData = {
        Username : username,
        Password : password,
        };
            var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(_poolData);
            var userData = {
             Username : username,
             Pool : userPool
            };
            cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
            var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
            cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: function (result) {
                    console.log('access token + ' + result.getAccessToken().getJwtToken());
                    console.log('idToken + ' + result.idToken.jwtToken)
                    deferred.resolve('successfully logged in.');
                },
                onFailure: function(err) {
                    console.log(error);
                    alert(err);
                    deferred.reject('login failled.');
                },
            });
            return deferred.promise;
        },

Because I can not get my user attributes after using this login method. Like this:

getCognitoUserAttr: function(username, _poolData) {
            var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(_poolData);
            var userData = {
             Username : username,
             Pool : userPool
            };
            cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
            cognitoUser.getUserAttributes(function(err, result) {
                if (err) {
                        alert(err);
                        return;
                }
                for (var i = 0; i < result.length; i++) {
                        console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue());
                }
            });
        }

I always have the error message:

Error: User is not authenticated

Note that the login method is from :https://docs.aws.amazon.com/fr_fr/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html

What I have to do?

Upvotes: 0

Views: 1888

Answers (1)

Chris
Chris

Reputation: 82

Here you go. I made a function called getUserAttributes() then used the same code as seen in isAuthenticated.

getUserAttributes(){
    let cognitoUser = this.getCurrentUser();

    if (cognitoUser != null) {
        cognitoUser.getSession(function (err, session) {

            cognitoUser.getUserAttributes(function(err, result) {
                if (err) {
                    console.log(err);
                    return;
                }
                for (let i = 0; i < result.length; i++) {
                    console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue());
                }
            });

        });
    }

}

this is the login function i use

authenticate(username: string, password: string, callback: CognitoCallback) {

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

        let authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

        let poolData = {
            UserPoolId : CognitoUtil._USER_POOL_ID,
            ClientId : CognitoUtil._CLIENT_ID
        };

        let userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

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

        let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

        cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: function (result) {

                AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                    IdentityPoolId : CognitoUtil._IDENTITY_POOL_ID,
                    Logins : {
                        'cognito-idp.REGION.amazonaws.com/POOLID':result.getIdToken().getJwtToken()
                    }
                });

                callback.cognitoCallback('loginSuccess', null);

            },

            onFailure: function (err) {
                callback.cognitoCallback(err.message, null);
            }

        });
    }

Upvotes: 1

Related Questions