当错误消息说userName和password不匹配时,如何通过Cognito对用户池中的用户进行身份验证?

时间:2016-06-25 04:35:58

标签: javascript amazon-web-services authentication amazon-cognito

所以我在使用Javascript中的Cognito及其Beta用户池功能。由于此处提供的文档,我成功创建了用户:

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

当我尝试对用户进行身份验证时,我收到一条奇怪的错误消息:

NotAuthorizedException:用户名或密码不正确。

我不明白为什么会出现这种情况,因为我实际上是在复制和粘贴用于创建用户的userName和password的值,这样可以正常工作。

以下是我验证用户身份的代码:

//----- 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);
        },

    });

你觉得这里有什么问题吗?我想也许我在这两个地方的UserName参数是一个不同的值或什么:

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

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

由于

1 个答案:

答案 0 :(得分:2)

如果您未确认新注册的用户,则不会显示您的代码。如果你不是,这将解释NotAuthorizedExceptionThis example具体是你想要遵循的。

如果您不想确认从您的应用中注册的用户,您应该使用PreSignUp triggerautoConfirm您的用户。