Search code examples
amazon-web-servicesaws-lambdamigrationamazon-cognitoamazon-cognito-triggers

AWS Cognito node lambda migration user : authenticateUser is not defined


I would like to migrate users from userPool 1 to userPool 2 with the migration user lambda in AWS Console function. In order to do it, I have used the script provided by AWS but I can't find how I can use authenticateUser for instance. It is not defined when executed.

The migration lambda is executed. authenticateUser is not defined

I have also tried to create a layer, imported succesfully and set the layer in my lambda function but cannot make it work too.

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 is not defined

My question is : how do we import this function ?

Thanks a lot.


Solution

  • That sample code is for migrating a user from a legacy database, and the authenticateUser, lookupUser functions are just abstractions for your business logic (which AWS can't write for you). For instance if you have to migrate from a legacy database (not a user pool), then you would lookup their user in your table, grab their salt, hash the password passed in to the migration trigger using the same logic you did in your legacy authentication method, compare it against the stored hashed password in your legacy database, etc. (It gets a little simpler if you were storing passwords in plaintext, but let's not consider that.)

    Here's a snippet that should do most of the migration for you. Someone asked a similar question on Github and referenced this StackOverflow issue.

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