Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a user just Google signed-in for the first time in Firebase

I have an Android app that already has Email and Password authentication using Firebase. For the user's ease, I have now added Google Sign-In in the project as well.
The problem is that when I log in using email, then the app creates some data on Realtime Database under key as per UID (Unique User ID - Acquired By Firebase Auth) of the user. This data is generally created at the time of the user's registration. But while, when users use the Google sign-in feature, they are authenticated through another activity hosted by google play services. I am unable to add this data.
So, the question arises that what should I do now to check if the user just did the login with a Google account or not. Also, if the user does google login for the first time, it will create the new data at the real-time database, else it will retrieve the data.
Please help me as I am stuck.

like image 209
Anuj Kumar Avatar asked Aug 17 '17 07:08

Anuj Kumar


1 Answers

In javascript, the following method firebase.auth().signInWithCredential returns firebase.auth.UserCredential which has a property additionalUserInfo.

additionalUserInfo has a boolean property called isNewUser.

You can place a simple check on isNewUser to check if the user is signing in for the first time.

like image 115
Saad Zafar Avatar answered Oct 29 '22 00:10

Saad Zafar