AngularJS 上的 AWS Cognito 登录错误

AWS Cognito login error on AngularJS

我正在使用 cognito Amazon Webservice 进行用户管理。

我已经管理了对我的用户池的签名,但每次我尝试登录到我的用户池时,我都会遇到此控制台错误:

Error: this.pool.getUserPoolId is not a function

我不知道从哪里获取 getUserPoolId...

编辑:我在一个工厂里有我的登录功能,这是我的代码:

login: function(username, password) {
        var authenticationData = {
                Username : username,
                Password : password
        };
        var userData = {
                Username :username,
                Pool : poolData
        };
        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());
            /*Use the idToken for Logins Map when Federating User Pools with Cognito Identity or when passing through an Authorization Header to an API Gateway Authorizer*/
            console.log('idToken + ' + result.idToken.jwtToken);
    },

    onFailure: function(err) {
            alert(err);
    },

        });
        }

有谁知道该怎么做吗?

我假设您正在初始化 poolData 对象:

var poolData = {
    UserPoolId : '...', // Your user pool id here
    ClientId : '...' // Your client id here
};

AWS Cognito JavaScript SDK 初始化模式要求您将 "data objects" 传递给 AWS SDK 构造函数。在 return 中,SDK 返回一个具有各种连接方法的 AWS "interface"。

我认为从您的代码中您使用了 data object 而不是随后的 interface

示例:

// Data Object used for initialization of the interface
const userPoolData = {
    UserPoolId: identityPoolId,
    ClientId: clientId
}

// The `userPoolInterface`, constructed from your `poolData` object
const userPoolInterface = new AWSCognito
    .CognitoIdentityServiceProvider
    .CognitoUserPool(userPoolData)

在您提供的代码中,您似乎正在传递 userData 对象(用于初始化),而您应该在其中传递之前应该已初始化的 userPool 接口。

试试这个:

login: function(username, password) {

    // 1) Create the poolData object
    var poolData = {
        UserPoolId: identityPoolId,
        ClientId: clientId
    };

    // 2) Initialize the userPool interface
    var userPool = new AWSCognito
        .CognitoIdentityServiceProvider
        .CognitoUserPool(poolData)

    // 3) Be sure to use `userPool`, not `poolData`
    var userData = {
        Username : username,
        Pool : poolData,  // <-- Data?! Oops..
        Pool : userPool  // "interface", that's better :)
    };

    var authenticationData = {
        Username : username,
        Password : password
    };

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

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

    cognitoUser.authenticateUser( ...etc... );
}

在线试用:

如果有帮助,我会在浏览 AWS Cognito SDK 示例时做笔记并制作示例。欢迎您查看我正在使用的 Github 存储库。如果您可以测试一个工作示例,它可能会有所帮助。

Github Repository / Live Demo