Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if user is authenticated for the first time in Firebase Google Authentication in Android

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?

like image 770
rainman Avatar asked Sep 17 '16 18:09

rainman


People also ask

How do you check if a user is authenticated in Firebase?

You can easily detect if the user is logged or not by executing: var user = firebase. auth().

How do I check if my phone number is registered in Firebase authentication using flutter?

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.

How do I check my Firebase auth token?

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.


1 Answers

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

like image 78
Eric Tjitra Avatar answered Sep 18 '22 18:09

Eric Tjitra