Osama
Osama

Reputation: 523

Firebase phone authentication : once phone entered check if there is a user with that phone in users

Hello every one i am trying to make phone authentication for the login and using signup with email and password when the user is signed up it stores email and password in the firebase default users and also add new firebase object with additional user information Phone , Car information , Name

enter image description here

now for the login the user enters their phone but (but) i don't want just any user who isn't signed up before to login and get authenticated i want to check if there is a user phone number as entered exists in the database

i cant use the UID because the user is not logged in yet what to do ?

Upvotes: 0

Views: 542

Answers (1)

user1872384
user1872384

Reputation: 7147

Osama, during sign up, you need to authenticate the user using phone (+xxxxxxxxxx).

The next immediate step you need is to link this phone with the email and password.

So let's say the user has sign out now. And he wants to sign in again.

1) e.g. he's in the phone number view, and he keys in his phone number (+xxxxxxxxxx), after he keys in the sms code correctly, you need to check if this phone is link to any other email password credentials

In the FIRAuth auth signInWithCredential using phone, you can check the FIRAdditionalUserInfo for "phone" and "password" to know if it's already linked to a password and prompt him to login.

2) e.g. he chooses to sign in (it should only ask for his email and password)

Updates:

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG, "signInWithCredential:success");

                    FirebaseUser user = task.getResult().getUser();
                    // ...get the user.providerData and FIRAdditionalUserInfo providerID "password"

                } else {
                    // Sign in failed, display a message and update the UI
                    Log.w(TAG, "signInWithCredential:failure", task.getException());
                    if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                        // The verification code entered was invalid
                    }
                }
            }
        });
}

Upvotes: 0

Related Questions