Adam Chaggama
Adam Chaggama

Reputation: 11

Xcode Swift Firebase Auth using phone number and password

I have been doing loads of research on this and i could'nt find anything that suits my needs. What i want to do is allow the user to sign in to my app using either his email or phone number and password like on Twitter and facebook. I know that there is no way to do that using firebase so i want to come up with an illusion of that happening.

I have come up with this:

  1. Store email, password and phone number in the firebase database under the user(branch name would be the UID of the user) during the sign up process and create the account on firebase using the email and password.

  2. In the login function check if the user is typing a phone number or an email address.

  3. If the user is typing an phone number, query the database to find the UID of the user that is linked to the number.

  4. If a user with that phone number exists then query the database to find out what the users email address and password is.

  5. Check if password provided is equal to the password stored on the database.

  6. Use the email address and password from the database to sign the user in using the regular firebase sign in function.

Can anyone tell me if this could work and what your thoughts are? I feel like this could work if coded properly. Would be good to know what other coders think before actually trying to code this out

Thanks guys!!

Upvotes: 1

Views: 977

Answers (1)

ZassX
ZassX

Reputation: 1379

You are right - you will have to come up with something custom, because Firebase doesn't support logins like that.

The idea is good, but I would make some security improvements to it:

  1. Don't store email!!
  2. Detect if user is typing in email or phone number. If user is trying to log in with phone number, query user's email address from database and save it to some variable.

Now since you have email address (you got it by querying it by users phone number) and password (which was inserted by user), you can perform login with function below:

Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
  // ...
}

Upvotes: 1

Related Questions