I am using Firebase Authentication in an Android application, and I am using Google account authentication as an option to sign in to the application.
How can I know if the user is signed in to the application for the first time or not?
You can easily detect if the user is logged or not by executing: var user = firebase. auth().
After you set up admin, you can use cloud_functions package to call APIs from the firebase admin SDK and the API we'll be using is one that allows us to get a user by phone number (documentation). If the API response is a user record, we know a phone exists. Save this answer.
To do so securely, after a successful sign-in, send the user's ID token to your server using HTTPS. Then, on the server, verify the integrity and authenticity of the ID token and retrieve the uid from it. You can use the uid transmitted in this way to securely identify the currently signed-in user on your server.
To check if it's the first time user logs in, simply call the AdditionalUserInfo.isNewUser()
method in the OnCompleteListener.onComplete
callback.
Example code below, be sure to check for null.
OnCompleteListener<AuthResult> completeListener = new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { boolean isNew = task.getResult().getAdditionalUserInfo().isNewUser(); Log.d("MyTAG", "onComplete: " + (isNew ? "new user" : "old user")); } } };
Check the docs for more reference AdditionalUserInfo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With