Search code examples
amazon-web-servicesamazon-cognitoaws-amplifyamazon-snsamazon-ses

Customizing OTP Authentication flow with AWS Amplify


I've currently integrated an OTP authentication signin flow to a React Native app with Amplify. The flow is as follows.

(I followed the guide provided in this article)

enter image description here

But right now I'm in the need of providing the option of sending the OTP code to a user entered email address in case if it is not received to the mobile number.

Initial thought was to trigger the create auth challenge lambda function via Amplify Auth.signUp by sending email attribute.

import Auth from '@aws-amplify/auth';

const sendVerificationToEmail = async (phone: number, email: string, password: string) => {
  await Auth.signUp({ 
      username: phone, 
      password, 
      attributes: { 
         email 
      } });
} 

Then the lambda function can be remodified as follows to either send an email or SMS,

...

const email = event.request.userAttributes.email;

if (email) {
  // Logic to send OTP verification code via SES
} else {
  // Logic to send OTP via SNS
}

but this is not possible since we already have a user created in the cognito user pool. This attribute will not be passed to the lambda function.

What is the correct approach of achieving this ?


Solution

  • Easiest approach is, Setup lambda function for custom flow.

    1. Create custom method to generate an OTP and save it to into a table and then use SES to send OTP to email .

    2. Create custom method to verify OTP.

    3. Check OTP from table and verify it.

     if(SUCCESS)
            use admin to mark as verified
    

    https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminConfirmSignUp.html