Cognito - How to give users Log in with both Email and Phone number
Asked Answered
N

3

8

I have cognito set up to allow both email addresses and phone numbers. Here is the the scenerio.

  • Sign up User A with email [email protected] and auto verify the email using CognitoIdentityServiceProvider#signup with username : [email protected] and UserAttributes for the email address.
  • Update the users account with a phone number 123-456-7899 and auto verify the phone number
  • Attempt to login with 123-456-7899
  • Instead of logging in user A which currently has a verified phone number of 123-456-7899 it creates user B

Is there any way to let users have unique phone numbers and emails and be able to login in with both? Or does the signup always go based on the initial username?

Nanna answered 10/3, 2019 at 18:46 Comment(1)
Check if user A gets disabled when user B is createdSoupspoon
R
3

It looks like your user pool is configured with UsernameAttributes and you chose both email and phone_number to act as the username. I agree that the behaviour is quite strange and you can end up with two different users, both having exactly the same email and phone number, both attributes verified.

Have you tried configuring the user pool with UsernameAliases? It allows you to specify 3 attributes (email, phone_number, preferred_username) that will act as username aliases and the users will be able to log in with any of them (assuming that they're verified). The caveat is that they won't be able to authenticate with the email address immediately after signing up as it needs to be verified first.

Roller answered 12/3, 2019 at 11:55 Comment(0)
B
1

you need to choose this option while setting up cognito user pools

  • Users can use a username and optionally multiple alternatives to sign up and sign in. Here you can allow users to sign in by email and sign in with a phone number.

Please note email & phone number both should be verified.

enter image description here

Bandler answered 19/7, 2022 at 6:35 Comment(0)
V
0

Here how you can login with both email/username:

Cognito With Lambda Function:

Make sure you enable both username and email options when creating a users pool:

enter image description here

if you want to log in with email, verify the user email with pre-signup lambda functions, or through the email verification code sent to them, and then verify their email address with lambda functions.

Verify email address through pre-signup lambda trigger :

    exports.handler = (event, context, callback) => {
    event.response.autoConfirmUser = true;
    event.response.autoVerifyEmail = true;
    context.done(null, event);
};

in your pre-signup lambda trigger.

if the email is not verified and you are trying to log in with the email you will get the error.

Email should be verified like in the image below: enter image description here

Vasya answered 16/2, 2023 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.