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.

- 1,092
- 1
- 12
- 26
5 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.

- 238
- 3
- 8
Try, additionalUserInfo.isNewUser:
var provider = new firebase.auth.GoogleAuthProvider();
firebase
.auth()
.signInWithPopup(provider)
.then((result) => {
console.log(result.additionalUserInfo.isNewUser)
})

- 41
- 2
Add some sort of flag in database while signing in for first time and make it true. Whenever user login, check if that flag exist and is true, then retrieve old data else add new data (or whatever you want).
firstlogin: true
this is javascript code with facebook signin, you may have an idea.
var provider = new firebase.auth.FacebookAuthProvider();
function facebookSignin() {
firebase.auth().signInWithPopup(provider)
.then(function(result) {
var user = result.user;
firebase.database().ref('/users/'+user.uid).on('value',
function(snapshot){
if (!snapshot.exists()) {
//user does not exist, add new data
}else{
//user exist, retrieve old data
}
}); //firebase.database().ref ends
});//then() ends
}//function ends

- 3,160
- 10
- 43
- 77
-
i did not tried like this.I referenced to a location where data is saved after getting an instance as per UID. But this doesn't work. can you help me to sort out this issue as per my comment – Anuj Kumar Aug 17 '17 at 07:27
-
I know About Value Event Listeners In Firebase Android. Can You Briefly Explain Me The Above code Snippet As Per Java. – Anuj Kumar Aug 17 '17 at 07:41
-
When User Successfully Signs Into App Using Google.The Code Goes To `updateUI()` method – Anuj Kumar Aug 17 '17 at 07:42
-
Make a node in database `signinflags` --- Let the user SignIn first ---- Then get UserID that recently SignedIn ---- Then Check `signinflags` node if user exist there ---- If yes then retrieve old data ---- if NO then push this userID in `signinflags` Node. Simple – Noman Ali Aug 17 '17 at 07:47
You need to save the shared preference and check for it whether the user is logged-in first time or not.
this link may help you out. But before see into the code just read the documentation for FIREBASE.

- 1,683
- 1
- 17
- 28
-
this will create a new pair of keys for same user if app is reinstalled or on other device – Anuj Kumar Aug 17 '17 at 07:32
-
As I told earlier I did not tried like this.I referenced to a location where data is saved after getting an instance as per UID. But this doesn't work. can you help me to sort out this issue as per my comment – Anuj Kumar Aug 17 '17 at 07:36
Yay! I found a way which works if data is already present on database or not. It works as I intended, If I create a new user from google/email, I save their name in real time database, but signing in from same account, earlier was leading to rewriting of new data (Name) on the same place - too much use of data bandwidth. I fixed by this. It simply checks where data exists there or not and then it further moves ahead.
final User newUser = new User();
newUser.email = user.getEmail();
newUser.name = user.getDisplayName();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("user/" + user.getUid())) {
//User Exists , No Need To add new data.
//Get previous data from firebase. Take previous data as soon as possible..
return;
} else {
FirebaseDatabase.getInstance().getReference().child("user/" + user.getUid()).setValue(newUser);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I have one thing to tell you guys, User is a custom class with POJOs to put data at firebase database. Reply me if this works for you too.
Edit - Firestore
newUser.email = user.getEmail();
newUser.name = user.getDisplayName();
DocumentReference docRef =
db.collection("users").document(user.getUid());
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull
Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
//User Exists , No Need To add new data.
//Get previous data from firebase. Take previous data as soon as possible..
} else {
Log.d(TAG, "No such document");
//Set new data here
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});```

- 1,092
- 1
- 12
- 26
-
How to try the same logic on new Firestore instead of RealtimeDatabase – Pallav Chaudhari Jul 22 '20 at 14:47
-
I have edited my answer. I hope Firestore code snippet will help you.. – Anuj Kumar Aug 23 '20 at 02:03
-
I have same issue. I only use google sign in method. How can i see signed users in firebase auth page? I only want to control user signs in auth. google signed in users do not appear inside firebase! – Handelika Jan 08 '21 at 10:58
-
-
@AnujKumar, Thanks. I found the solution with `mAuth.signInWithCredential(credential)` – Handelika Jan 09 '21 at 14:12