简体   繁体   中英

How to resend confirmation code in Cognito's sign-up process

I'm having issues with resending the confirmation code for cases where for some reason the confirmation code was not delivered to the users. This is what I have:

First, I have a sign-up step:

const cognito = new AWS.CognitoIdentityServiceProvider({region});
const params = {
    ClientId,
    Username,
    Password,
};
const result = await cognito.signUp(params).promise();

This step (in case of success) should send an email to the user's email address (which is also their username) with their confirmation code. Now let's assume for some reason that email is not sent (the reason itself is not important). I would like to provide a chance to the user to ask for a new email to be sent. These are what I've been testing for this purpose so far:

First, I've tested the resendConfirmationCode method:

const cognito = new AWS.CognitoIdentityServiceProvider({region});
const params = {
    ClientId,
    Username,
};
const result = await cognito.resendConfirmationCode(params).promise();

Executing this code throws this error message:

UnhandledPromiseRejectionWarning: NotAuthorizedException: Cannot resend codes. Auto verification not turned on.

Then, I tested this approach (because of the answer given in this post ):

const cognito = new AWS.CognitoIdentityServiceProvider({region});
const params = {
  UserPoolId,
  Username,
  DesiredDeliveryMediums: ["EMAIL"],
  ForceAliasCreation: false,
  MessageAction: "RESEND",
}
const result = await cognito.adminCreateUser(params).promise();

This time, I'm getting this error:

UnhandledPromiseRejectionWarning: UnsupportedUserStateException: Resend not possible. ********-****-****-****-********** status is not FORCE_CHANGE_PASSWORD

The retracted part is the user's sub.

So, does anyone know how I can resend the confirmation code for a user that is not confirmed yet?

For anyone else who might be facing this issue, this was because the User Pool's setting was not to sent the verification code in the first place. Here's how to enable sending the verification code on User Pool:

  1. Go to Coginto and the User Pool
  2. Go to the page "MFA and verifications"
  3. In the section "Which attributes do you want to verify?"select one of the items (for me it was "Email")

But the actual issue for me was that this option was initially set properly earlier. But it was reset to "No verification" when I executed this command:

aws cognito-idp update-user-pool \
    --region us-east-1 \
    --user-pool-id us-east-1_********* \
    --lambda-config CustomMessage=arn:aws:lambda:us-east-1:************:function:composeEmail

This command is supposed to introduce a lambda function to compose the email for verification code. But for some reason, it will reset the other setting as well and I have no idea why.

In any case, once you have that setting set properly, my first solution will work:

const cognito = new AWS.CognitoIdentityServiceProvider({region});
const params = {
    ClientId,
    Username,
};
const result = await cognito.resendConfirmationCode(params).promise();

After a chat with AWS support, you can specify verification attribute like this:

aws cognito-idp update-user-pool \
    --region us-east-1 \
    --user-pool-id us-east-1_********* \
    --lambda-config CustomMessage=arn:aws:lambda:us-east-1:************:function:composeEmail
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM