Reputation: 1162
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.
Upvotes: 7
Views: 7874
Reputation: 1162
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());
}
}
});```
Upvotes: 0
Reputation: 41
Try, additionalUserInfo.isNewUser:
var provider = new firebase.auth.GoogleAuthProvider();
firebase
.auth()
.signInWithPopup(provider)
.then((result) => {
console.log(result.additionalUserInfo.isNewUser)
})
Upvotes: 4
Reputation: 258
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.
Upvotes: 6
Reputation: 3340
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
Upvotes: 0