问题
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?
回答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.
来源:https://stackoverflow.com/questions/61236899/how-to-check-when-a-user-has-phone-authenticated-in-firebase