简体   繁体   English

AWS Cognito无法进行身份验证:空白答案-JS SDK

[英]AWS Cognito Unable to authenticate : blank answer - JS SDK

So I used this chunk of code found on the official repo of AWS JS SDK. 因此,我使用了在AWS JS SDK官方存储库中找到的这段代码。 It is used to authenticate a user. 它用于验证用户。

It is returning a blank response. 它返回一个空白响应。

AWSCognito.config.region = 'us-east-1';
var authenticationData = {
    Username : '+1112223333', //some phone number used as an Alias
    Password : 'password123456',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var poolData = {
    UserPoolId : 'us-east-1_P00l1d', // Your user pool id here
    ClientId : 'xxx' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
    Username : 'myusername',
    Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId : 'xxx', // your identity pool id here
            Logins : {
                // Change the key below according to the specific region your user pool is in.
                'cognito-idp.pool_id_number_here_xxx' : result.getIdToken().getJwtToken()
            }
        });
    },

    onFailure: function(err) {
        alert(err)
        console.log("Error " + err);
    },

});

So, for the authenticationData, I used the phone number as username (phone number is set as an alias) and the password. 因此,对于authenticationData,我使用电话号码作为用户名(电话号码设置为别名)和密码。 I tried with the username directly as well. 我也尝试直接使用用户名。 The UserPoolId and ClientId are correct as I used the same value for registering and confirming the phone number. UserPoolId和ClientId是正确的,因为我在注册和确认电话号码时使用了相同的值。 In the userData, I set the Username with the proper myusername. 在userData中,将Username设置为正确的myusername。

About the Identity Pool, I created an Identity Pool on AWS Console linked to my App and my UserPool and I replaced the values IdentityPoolId and Logins in authenticateUser. 关于身份池,我在与我的App和UserPool链接的AWS控制台上创建了一个身份池,并在authenticateUser中替换了IdentityPoolId和Logins值。 I am not completely sure about the value in Logins though. 我不太确定“登录名”中的值。 I used cognito-idp.POOLIDNUMBER. 我使用了cognito-idp.POOLIDNUMBER。

The output from AWS is blank. AWS的输出为空白。 I am thinking that I can not even reach the server and I suspect an issue with the roles or the Identity Pool (the userPool is fine I suppose). 我以为我什至无法到达服务器,而且我怀疑角色或身份池有问题(我认为userPool很好)。

My identity pool is only using AWS Cognito users, not Facebook or other Identity Providers. 我的身份池仅使用AWS Cognito用户,而不使用Facebook或其他身份提供商。

用--with-all重新编译SJCL解决了该问题。

Please make sure you have all the necessary libraries for 'Amazon Cognito Identity SDK for JavaScript'. 请确保您具有“适用于JavaScript的Amazon Cognito身份SDK”的所有必需库。 Following is the list and links to these libraries. 以下是这些库的列表和链接。

1.The Amazon Cognito AWS SDK for JavaScript - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js 1.亚马逊AWS Cognito SDK为JavaScript - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js

2.The Amazon Cognito Identity SDK for JavaScript - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js 2.适用于JavaScript的Amazon Cognito身份SDK- https: //raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js

3.JavaScript BN library (jsbn.js,jsbn2.js) - http://www-cs-students.stanford.edu/~tjw/jsbn/ 3.JavaScript BN库(jsbn.js,jsbn2.js) - http://www-cs-students.stanford.edu/~tjw/jsbn/

4.Stanford JavaScript Crypto Library - https://github.com/bitwiseshiftleft/sjcl 4,斯坦福JavaScript加密库-https: //github.com/bitwiseshiftleft/sjcl

5.AWS SDK for JavaScript (optional, to use other aws services) - http://aws.amazon.com/sdk-for-browser/ 5.AWS SDK对JavaScript(可选,使用其他AWS服务) - http://aws.amazon.com/sdk-for-browser/

Include all these libraries and then use the code snippet below. 包括所有这些库,然后使用下面的代码段。

AWSCognito.config.region = 'YOUR_REGION';
var poolData = { 
    UserPoolId : 'YOUR_USER_POOL_ID', // Your user pool id here
    ClientId : 'YOUR_CLIENT_ID' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
    Username : "userName", // your username here
    Pool : userPool
};
//Signing Users in the App
var authenticationData = {
    Username : "userName", // your username here
    Password : "password" // your password here
};

var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);

var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用AWS Cognito SDK使用NodeJS从REST服务验证用户? - How to use AWS Cognito SDK to authenticate user from REST Service using NodeJS? AWS Cognito:生成令牌并在使用 amazon-cognito-identity-js SDK 刷新后 - AWS Cognito: Generate token and after refresh it with amazon-cognito-identity-js SDK 如何使用AWS Cognito在浏览器中验证用户? - How to authenticate user in browser using AWS Cognito? 如何使用AWS Cognito对API网关进行身份验证 - How to use AWS Cognito to authenticate API Gateway 使用来自网站 (vue.js) 中生成的 UI 的 AWS Cognito Id 令牌来验证对 AWS Gateway 的 HTTP 请求 - Using AWS Cognito Id Token from generated UI in a website (vue.js) to authenticate HTTP Requests to AWS Gateway 来自 Java 脚本 SDK 的 AWS Cognito adminCreateUser - AWS Cognito adminCreateUser from java script SDK 移动应用中的Cognito DynamoDB AWS SDK - Cognito DynamoDB aws sdk in mobile app Vanilla Js 和 AWS Cognito (AWS Amplify) - 安全吗? - Vanilla Js and AWS Cognito (AWS Amplify) - Is it safe? 使用 Cognito 用户池向 AWS AppSync 验证 Apollo 客户端 - Authenticate Apollo Client to AWS AppSync with Cognito User Pools 如何针对AWS Cognito用户池进行身份验证 - How do I authenticate against an AWS Cognito User Pool
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM