Mark Keane
Mark Keane

Reputation: 1044

How do you authenticate a user in a user pool via Cognito when error messages say userName and password don't match even though they do?

So I am messing around with Cognito and their Beta User Pools feature in Javascript. I am successfully creating users thanks to the documentation found here:

http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html

When I try and authenticate the users though I get a strange error message saying:

NotAuthorizedException: Incorrect username or password.

I don't understand why this is the case as I am literally copying and pasting the values for userName and password that I used to create the user, which is working fine.

Below is my code to authenticate the user:

//----- Setup
AWS.config.region = 'us-east-1'; // Region
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'myIdentityPoolId'
    });

    AWSCognito.config.region = 'us-east-1';

    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'myIdentityPoolId'
    });

    var poolData = { UserPoolId : 'myUserPoolId',
        ClientId : 'myClientId'
    };

//-----Authenticate a user
    alert('Authenticate a user with application');
    var authenticationData = {
        Username : 'markie17061993',
        Password : 'MeowWoof123456789!',
    };
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId : 'myUserPoolId',
        ClientId : 'myClientId'
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
        Username : 'markie17061993',
        Pool : userPool
    };
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    alert('hiss');
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
        },

        onFailure: function(err) {
            alert(err);
        },

    });

Do you see anything wrong here? I thought perhaps my UserName parameter in these two places is meant to be a different value or something:

var authenticationData = {
        Username : 'markie17061993',
        Password : 'MeowWoof123456789!',
    };

var userData = {
        Username : 'markie17061993',
        Pool : userPool
    };

Thanks

Upvotes: 1

Views: 3199

Answers (1)

Chetan Mehta
Chetan Mehta

Reputation: 5661

Your code doesn't show if you are confirming the newly signed up user not. If you are not, that would explain the NotAuthorizedException. This example specifically is what you want to follow.

If you do not want to confirm the users being signed up from you app, you should use a PreSignUp trigger to autoConfirm your users.

Upvotes: 2

Related Questions