【问题标题】:AWS Cognito Unable to authenticate : blank answer - JS SDKAWS Cognito 无法进行身份验证:空白答案 - JS SDK
【发布时间】:2016-12-07 10:41:37
【问题描述】:

所以我使用了在 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);
    },

});

因此,对于 authenticationData,我使用电话号码作为用户名(电话号码设置为别名)和密码。我也直接尝试使用用户名。 UserPoolId 和 ClientId 是正确的,因为我使用相同的值来注册和确认电话号码。 在 userData 中,我将用户名设置为正确的 myusername。

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

AWS 的输出为空白。 我在想我什至无法访问服务器,并且我怀疑角色或身份池存在问题(我想 userPool 很好)。

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

【问题讨论】:

    标签: javascript amazon-web-services cloud aws-sdk amazon-cognito


    【解决方案1】:

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

    【讨论】:

    • 应该是编辑或评论,但不是答案
    【解决方案2】:

    请确保您拥有“适用于 JavaScript 的 Amazon Cognito 身份开发工具包”的所有必要库。以下是这些库的列表和链接。

    1.适用于 JavaScript 的 Amazon Cognito AWS 开发工具包 - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js

    2.适用于 JavaScript 的 Amazon Cognito 身份开发工具包 - 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.适用于 JavaScript 的 AWS 开发工具包(可选,用于使用其他 aws 服务)- http://aws.amazon.com/sdk-for-browser/

    包含所有这些库,然后使用下面的代码 sn-p。

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

    【讨论】:

    • 有帮助吗...?
    猜你喜欢
    • 2018-06-03
    • 2018-03-14
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2023-04-03
    • 2020-12-31
    相关资源
    最近更新 更多