AWS Cognito 无法验证:空白答案 - JS SDK

AWS Cognito Unable to authenticate : blank answer - JS SDK

所以我使用了在 AWS JS SDK 的官方仓库中找到的这段代码。 它用于验证用户。

返回空白响应。

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

});

因此,对于身份验证数据,我使用 phone 号码作为用户名(phone 号码设置为别名)和密码。我也直接尝试使用用户名。 UserPoolId 和 ClientId 是正确的,因为我使用相同的值来注册和确认 phone 号码。 在 userData 中,我使用正确的 myusername 设置了用户名。

关于身份池,我在 AWS 控制台上创建了一个身份池,该控制台链接到我的应用程序和我的 UserPool,并且我替换了 authenticateUser 中的值 IdentityPoolId 和 Logins。 不过,我不完全确定登录的价值。我使用了 cognito-idp.POOLIDNUMBER.

AWS 的输出为空白。 我在想我什至无法连接到服务器,我怀疑角色或身份池有问题(我想 userPool 没问题)。

我的身份池仅使用 AWS Cognito 用户,不使用 Facebook 或其他身份提供商。

使用 --with-all 重新编译 SJCL 解决了这个问题。

请确保您拥有 'Amazon Cognito Identity SDK for JavaScript' 所需的所有库。以下是这些库的列表和链接。

1.The 适用于 JavaScript 的 Amazon Cognito AWS SDK - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js

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

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

4.Stanford JavaScript 加密库 - https://github.com/bitwiseshiftleft/sjcl

5.AWS JavaScript 的 SDK(可选,使用其他 aws 服务)- http://aws.amazon.com/sdk-for-browser/

包括所有这些库,然后使用下面的代码片段。

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