【问题标题】:AWS Cognito node lambda migration user : authenticateUser is not definedAWS Cognito 节点 lambda 迁移用户:authenticateUser 未定义
【发布时间】:2020-02-12 06:25:41
【问题描述】:

我想使用 AWS 控制台功能中的迁移用户 lambda 将用户从 userPool 1 迁移到 userPool 2。为了做到这一点,我使用了 AWS 提供的脚本,但我找不到例如如何使用 authenticateUser。执行时未定义。

执行迁移 lambda。 未定义身份验证用户

我也尝试创建一个图层,成功导入并在我的 lambda 函数中设置图层,但无法使其工作。

exports.handler = (event, context, callback) => {

    var user;

    if ( event.triggerSource == "UserMigration_Authentication" ) {

        // authenticate the user with your existing user directory service
        user = authenticateUser(event.userName, event.request.password);
        if ( user ) {
            event.response.userAttributes = {
                "email": user.emailAddress,
                "email_verified": "true"
            };
            event.response.finalUserStatus = "CONFIRMED";
            event.response.messageAction = "SUPPRESS";
            context.succeed(event);
        }
        else {
            // Return error to Amazon Cognito
            callback("Bad password");
        }
    }
    else if ( event.triggerSource == "UserMigration_ForgotPassword" ) {

        // Lookup the user in your existing user directory service
        user = lookupUser(event.userName);
        if ( user ) {
            event.response.userAttributes = {
                "email": user.emailAddress,
                // required to enable password-reset code to be sent to user
                "email_verified": "true"  
            };
            event.response.messageAction = "SUPPRESS";
            context.succeed(event);
        }
        else {
            // Return error to Amazon Cognito
            callback("Bad password");
        }
    }
    else { 
        // Return error to Amazon Cognito
        callback("Bad triggerSource " + event.triggerSource);
    }
};

authenticateUser 未定义

我的问题是:我们如何导入这个函数?

非常感谢。

【问题讨论】:

    标签: amazon-web-services aws-lambda migration amazon-cognito amazon-cognito-triggers


    【解决方案1】:

    该示例代码用于从旧数据库迁移用户,authenticateUser、lookupUser 函数只是您的业务逻辑的抽象(AWS 无法为您编写)。例如,如果您必须从旧数据库(而不是用户池)迁移,那么您将在表中查找他们的用户,获取他们的盐,使用您在旧数据库中执行的相同逻辑散列传递给迁移触发器的密码身份验证方法,将其与旧数据库中存储的散列密码进行比较,等等。(如果您以明文形式存储密码,它会变得更简单,但我们不要考虑这一点。)

    这是一个可以为您完成大部分迁移的 sn-p。有人在 Github 上问过类似的问题,并引用了这个 StackOverflow 问题。

    const AWS = require('aws-sdk');
    const cognitoIdentity = new AWS.CognitoIdentityServiceProvider({ region: '<your-region-here>' });
    
    const UserPoolId = process.env.deprecatedUserPoolId;
    
    exports.handler = async (event) => {
        const { userName } = event;
    
        const getUserParams = {
            Username: userName,
            UserPoolId
        };
    
        try {
            const user = await cognitoIdentity.adminGetUser(getUserParams).promise();
            //TODO: if you have custom attributes, grab them from the user variable and store them in the response below
            event.response = { finalUserStatus: "CONFIRMED" }
            return event;
        } catch (e) {
            throw e; //no user to migrate, give them an error in the client 
        }
    };
    

    【讨论】:

    • 非常感谢,我现在理解得更好了,可以使用这个 lambda 函数了! :)
    • 抱歉,这里的菜鸟完全被卡在了深处。创建 lambda 后,我们如何设置触发器配置。如果您选择 cognito,则必须提供身份池而不是用户池。
    • 您好@cham,如果您使用的是 AWS Cognito 控制台 UI,您可以在触发器页面上选择“用户迁移”并将其设置为与本文对应的 lambda。你评论的最后一点我不确定我是否理解。让我知道你是否仍然坚持这一点
    猜你喜欢
    • 2020-12-22
    • 2018-12-17
    • 1970-01-01
    • 2018-09-09
    • 2021-04-16
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多