6

During the user migration I want to return "Incorrect username or password." as error message instead of "User does not exist"

Have been searching on google for a while, cannot find out how to replicate the following JS example in this documentation

https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-migrate-user.html

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

It uses callback('message') in nodejs but I cannot find out how to do that in Python.

Stumbled on to this question

I can't find callback parameter in python lambda handler

Tried returning message string, but get "Exception during user migration"

6
  • Did you ever figure this out beyond using the custom UI / throwing exceptions? I am using node in a similar way to the default example you have and the callback logs the error messages to cloudwatch, but still returns the generic 'exception during user migration'. I thought maybe I could do context.fail('something'); ... but that just went to the logs, as well. Commented Nov 15, 2018 at 17:30
  • Was there any solution to this (for hosted UI) Commented Feb 26, 2019 at 11:32
  • 1
    No solution for hosted UI. Commented Feb 27, 2019 at 1:45
  • @james-lin can you put your code in Python please ? Commented Jul 6, 2020 at 19:03
  • @hernan I don't work for that company anymore so I don't have the code. Commented Jul 6, 2020 at 21:31

2 Answers 2

1

Try raising a ValueError:

raise ValueError("My custom message")

For me, this changed the wording of the exception returned to the boto client from

An error occurred (UserNotFoundException) when calling the AdminInitiateAuth operation: Exception migrating user in app client aeiouffhfha

to the somewhat more useful

An error occurred (UserNotFoundException) when calling the AdminInitiateAuth operation: UserMigration failed with error My custom message.
Sign up to request clarification or add additional context in comments.

Comments

0

So I have also tried raising an exception':

raise Exception('Incorrect username or password') 

This still gives "Exception during user migration" in the hosted UI, but that worked on a custom login UI which respects the error message.

2 Comments

I just tried this and all I got back from Cognito was {message: "Exception migrating user in app client 7qk...", __type: "UserNotFoundException"}. I also tried just returning a string value, with the same result. Did just raising an exception allow you to customize the response from Cognito?
at long as you do it normal UI (non-hosted) one, then it will show up properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.