1
votes

If I follow the example "Authenticate a User" shown on the http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html page or the "Use case 4" example on the https://github.com/aws/amazon-cognito-identity-js/blob/master/README.md page using the Username, the example works.

I am trying to authenticate a user using their email attribute, rather than their username. I have marked the email attribute as an Alias in my User Pool.

User Pools Console in AWS User Pools Console in AWS

When I use email in place of username in the "Authenticate a User" example I get the following error: ResourceNotFoundException: Username/client id combination not found. I have included my code sample below.

How does one Authenticate a User via email address using the "Amazon Cognito Identity SDK for JavaScript" (https://github.com/aws/amazon-cognito-identity-js)?

Code Sample

function authenticateUserViaEmail() {

log("authenticateUserViaEmail called");

// Initialize the Amazon Cognito credentials provider
AWS.config.region = 'us-east-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: identityPoolId,
});

AWSCognito.config.region = 'us-east-1';
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: identityPoolId,
});

var authenticationData = {
    Username : document.getElementById("email").value,
    Password : document.getElementById("password").value
};

log("using: " + JSON.stringify(authenticationData));

var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : userPoolId,
    ClientId : clientId
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
    Username : document.getElementById("email").value,
    Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

log("About to call authenticateUser...");

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        log('Access token: ' + result.getAccessToken().getJwtToken());
    },
    onFailure: function(err) {
        log(err);
        console.error(err);
    },
});            
}
1

1 Answers

3
votes

Did you confirm your email address? If you did not confirm your email address, it cannot be used as an alias for logging in.