custom error from aws cognito trigger lambda

Viewed 392

Is there a way to customize the error from cognito trigger lambda?

export async function handler(event: PreSignUpTriggerEvent, context: Context, callback: Callback<PreSignUpTriggerEvent>): Promise<void> {
    console.log(event);
    // Return to Amazon Cognito
    callback(message, event);            
}

This would return the following error to my callback url

http://localhost:3000/?error_description=PreSignUp+failed+with+error+Error%3A+User+not+exists+in+the+system.+Access+deny.+&error=invalid_request

I understand its not possible to change the error_description to remove the PreSignUp+failed+with+error prefix (reference).

However, is it possible to customize the error=invalid_request query param to error=access_denied or anything custom error other than invalid_request?

1 Answers

You can use this link as a base example and return a constant to identify the error on the client-side:

enter image description here

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

Example:

const error = new Error("EMAIL_MISSING");
callback(error, event);

Update

Like @srx said: the response of Cognito will be, via the query string, the error described by them plus your constant, like below: error_description=PreSignUp+failed+with+error+EMAIL_MISSING

So you can check on the client-side. On the snippet of a react app bellow I did it:

const locationSearch = String("error_description=PreSignUp+failed+with+error+EMAIL_MISSING");
const isAzureError = locationSearch.includes('AZURE_IDP_ERROR');
const isEmailMissing = locationSearch.includes('EMAIL_MISSING');
Related