如何使用 Lambda 函数和 AWS Cognito 对用户进行身份验证?

How to authenticate users using Lambda functions and AWS Cognito?

我正在尝试使用 AWS 中 Node.js 编写的 Lambda 函数对用户进行身份验证。我现在卡住了。 以下代码未按预期工作:

var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
    AWS.config.region = 'us-east-1';
      var authenticationData = {
        Username : 'username',
        Password : 'password',
    };
    var authenticationDetails = new AWS.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId : 'My-Pool-Id',
        ClientId : 'My-Client-Id'
    };
    var userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
        Username : 'username',
        Pool : userPool
    };
    var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
           
            console.log('idToken + ' + result.idToken.jwtToken);
        },

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

    });

};

以上代码在Cognito Documentation page

中提供

但我得到的只是以下错误:

TypeError: AWS.CognitoIdentityServiceProvider.AuthenticationDetails is not a function

有人知道我现在应该做什么吗? 谢谢

您正在使用 AWSCognito 但未定义。将其更改为 AWS.

您是否正在尝试为 Cognito 编写包装器?您可以在客户端而不是在 Lambda 中执行此身份验证。