2

I have an Android app where users are registered in the app with their phone number, I am using Firebase to store in Authentication their phone and their email and also I am saving in the Realtime Database their phone, their full name, and their email. The structure in the Realtime Database is as follows:

Auto-Generated ID 
+16505553434: "some@email.com"  
email:"some@email.com"  
first name: "First name"  
last name: "Last name" 
phone: "+16505553434"

After the user has registered and signed out when they try to use the app again I want to: if the user exists I don't want to do phone authentication again this should happen only once when they register if the user exists in the database I want to just type their password and log in. But the problem is how will I check if the user is phone registered in Firebase. If the user has registered I want to show a layout for the input password while if the user is not registered I want to show the OtpView so that the user to do phone authentication-registration. When the user has signed out the FirebaseAuth.getInstance().getCurrentUser() is null so i cannot use that. What can I do to check if the user is registered or not?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
cousma
  • 87
  • 1
  • 10

1 Answers1

1

After the user has registered and signed out when they try to use the app again I want to: if the user exists I don't want to do phone authentication again this should happen only once when they register if the user exists in the database I want to just type their password and log in.

When a user is authenticated with the phone number, there is no password involved. The authentication is made using the verification code that is sent via SMS. So if the user signs out, there is no way he can simply log-in using a password. He can log-in again using the phone number or any other provider.

But the problem is how will I check if the user is phone registered in Firebase. If the user has registered I want to show a layout for the input password while if the user is not registered I want to show the OtpView so that the user to do phone authentication-registration.

You can simply check your database against the phone number to see if the user already has an account. A query like this might do the trick:

db.child("users").orderByChild("phoneNumber").equalTo("+16505553434");

If you get a result, it means that the users exist. To be able to let the user "log-in with a password", you need to enable this kind of authentication. You can do it very easily in the Firebase console. But bear in mind that this is another type of authentication that cannot be combined with the first one. Check the docs regarding Authenticate with Firebase using Password-Based. So you can either sign-in with the phone number or with the email and password. You cannot sign-in with a phone number and password.

When the user has signed out the FirebaseAuth.getInstance().getCurrentUser() is null so i cannot use that. What can I do to check if the user is registered or not?

When the user signs out, the FirebaseUser object is null. There is no way you can get data from that object. All you can do it to query the database.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    thans for your reply. Actually i didn't mention that after the user has done the phone authentication then i ask them to add their email address and a password and then i use this method mAuth.getCurrentUser().linkWithCredential(credential) to link the email with their phone and the credential is AuthCredential credential = EmailAuthProvider.getCredential(email, password);. When i want the user to sign in i use this method mAuth.signInWithEmailAndPassword(emailInDB, password). the emailInDB i retrieve it from the realtime database having the phone they have authenticated. – cousma Apr 16 '20 at 09:43
  • You forget to mention the most important part. So using that, you got the expected behavior, right? – Alex Mamo Apr 16 '20 at 10:02
  • yes with the query i get the phone child on my user and it identifies that the user is registered, it works as expected. So there isn't a way to check from the authentication part that a particular phone has registered? I mean apart from the realtime database to check based on the Authentication section of the firebase console where a new user is displayed after they are registered.. – cousma Apr 16 '20 at 10:17
  • Good to hear that you got the expected behavior. No, there is not while signed out. You can only know [if the user is signed in by checking the provider](https://stackoverflow.com/questions/61156122/how-to-check-for-a-phone-auth-user-in-android/61156915#61156915). – Alex Mamo Apr 16 '20 at 10:19
  • ok the providerId tells me that the user has used PhoneAuthProvider.PROVIDER_ID to register but that does not identify the user in the Authentication i.e does not prove that the user has indeed registered in the database right? – cousma Apr 16 '20 at 10:43
  • Yes, that right. That information has nothing to do with the fact that you added the data to the database. – Alex Mamo Apr 16 '20 at 10:57
  • @cousma if you logout using `FirebaseAuth.getInstance().signOut();` and signIn again using realtime database data (email, password) what `FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();` returns??? Null or some value ??? – ibrahim Feb 26 '21 at 09:56