javascript - 亚马逊 Cognito JS 开发工具包

标签 javascript amazon-web-services amazon-cognito

我正在尝试使用他们的 JavaScript SDK 了解 Cognito 身份验证。我有以下代码(由 React 应用程序中的表单触发):

import {
    CognitoUserPool,
    AuthenticationDetails,
    CognitoUser
} from "amazon-cognito-identity-js";

const poolData = {
    UserPoolId: MY_POOL_ID,
    ClientId: APP_CLIENT_ID
};

export function cognitoLogin(credentials, callback) {
    const authenticationDetails = new AuthenticationDetails(credentials);

    const userPool = new CognitoUserPool(poolData);
    const cognitoUser = new CognitoUser({
        Username: credentials.username,
        Pool: userPool
    });

    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function(response) {
            console.log("SUCCESS!");
            console.log(response);
        },
        onFailure: function(error) {
            console.log("FAILURE!");
            console.log(error);
        }
    });
}

我生成了一个没有 secret 的客户端应用程序。我生成了一个用户并使用自定义域 UI 与该用户一起登录并确认。但是,当我尝试通过 SDK 登录时,我得到一个

{
    code: "NotAuthorizedException",
    message: "Incorrect username or password.",
    name: "NotAuthorizedException"
}

错误。我可能缺少什么?

最佳答案

credentials 对象必须包含键 UsernamePassword - 请注意它们的首字母大写,但您的似乎全是小写。

关于javascript - 亚马逊 Cognito JS 开发工具包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51449846/

相关文章:

javascript - 打开 URL 但指向主目录

amazon-web-services - AWS mediaconvert 视频缩略图生成器会旋转吗?

aws-api-gateway - 使用Chalice和Cognito授权者创建的Api返回 “Unauthorized”

javascript - 在触发事件上重新渲染 Backbone.Marionette 模板

javascript - 从 JavaScript INLINE 中去除非数字或小数

python - 无法从 AWS boto3 生成的签名 URL 下载

amazon-web-services - 从 Docker 容器中获取 AWS 实例元数据?

amazon-web-services - 使用Chalice将 header 添加到AWS API Gateway Response

oauth-2.0 - 如何在 Passport 中使用 AWS Cognito 作为提供商?

javascript - 空语句在 JavaScript 中作为空操作命令的实际应用