【问题标题】: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?当错误消息说用户名和密码不匹配时,如何通过 Cognito 对用户池中的用户进行身份验证?
【发布时间】:2016-10-27 18:11:48
【问题描述】:

所以我在搞乱 Cognito 及其 Javascript 中的 Beta 用户池功能。多亏了这里的文档,我成功地创建了用户:

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

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

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

我不明白为什么会这样,因为我实际上是在复制和粘贴用于创建用户的用户名和密码的值,这工作正常。

下面是我验证用户的代码:

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

谢谢

【问题讨论】:

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


    【解决方案1】:

    如果您不确认新注册的用户,您的代码不会显示。如果你不是,那将解释NotAuthorizedExceptionThis example 具体是你想关注的。

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

    【讨论】:

    • 当我设置用户池时,我没有选择电话或电子邮件验证,所以我认为不是这样,还是我遗漏了什么?这就是this所指的对吗?
    • 即使您没有这样做,默认情况下 Cognito 用户池也会在未确认/未验证状态下创建用户。如果您不希望您需要自动确认所有用户的 PreSignUp 触发器。
    • 非常感谢! ????
    • 你知道为什么 cognito 会这样做吗?
    • 谢谢,^ 这个例子帮助了我!
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 2018-02-23
    • 2016-08-18
    • 1970-01-01
    • 2019-09-30
    • 2018-12-19
    • 1970-01-01
    • 2017-04-16
    相关资源
    最近更新 更多