I’m using CDK for the infrastructure and Amplify for the front end. I’m new to it. So maybe I was missing something.
What I want is to send an email with a verification code to validate it when a user is created by the command AdminCreateUserCommand
and not the default invitation email. I use Amplify in the front end to handle the sign-in.
I implemented the configuration in the cognito constructor to verify the user’s email but it didn’t work.
I read the documentation to see how could I implement this feature, but I didn’t find anything about it. Some posts and blogs said that it’s not possible to do this thing using admin flow, I mean creating a user by AdminCreateUserCommand
. Only in normal flow, I mean when the user signs up themselves. Also, I added a trigger lambda to customize the message.
Note: the conditions work as expected.
The constructor cognito:
autoVerify: {
email: props.emailVerificationRequired
},
userVerification: props.userVerificationRequired
? {
emailSubject: 'Verify your email for our awesome app!',
emailBody:
'Hello {username}, Thanks for signing up to our awesome app! Your verification code is {####}',
emailStyle: cognito.VerificationEmailStyle.CODE
}
: undefined,
standardAttributes: {
email: {
required: true,
mutable: false
}
},
customAttributes: {
locale: new cognito.StringAttribute({ mutable: true })
},
lambdaTriggers: {
preSignUp: preSignUpLambda,
customMessage: props.userVerificationRequired ? customMessage : undefined
},
Lambda in charge of creating a user:
const userPoolId = process.env.COGNITO_USER_POOL_ID;
console.log('userPoolId', userPoolId);
const params = {
UserPoolId: userPoolId,
Username: validatedEmail,
DesiredDeliveryMediums: ['EMAIL' as const],
TemporaryPassword: validatedPassword,
UserAttributes: [
{
Name: 'locale',
Value: validatedLocale
},
{
Name: 'email',
Value: validatedEmail
}
],
ClientMetadata: {
appName: process.env.APP_NAME || 'myApp'
}
};
console.log('params', params);
try {
await cognitoClient.send(new AdminCreateUserCommand(params));