Reputation: 13
I need to know if the user who has just signed in with Google is a new user, so I can take an action needed for new users in my app.
I heard about this method isNewUser()
, but I'm actually not familiar with abstract methods and I don't know exactly how I can use it in my situation:
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo.html#isNewUser()
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
// TAKE ACTION IF IT IS A NEW USER.
} else {
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, R.string.authentication_failed,
Toast.LENGTH_SHORT).show();
}
}
});
Thanks in advance!
Upvotes: 1
Views: 302
Reputation: 30818
There is actually a better way to do this now.
To check if a user is new or existing after sign in, use isNewUser
:
https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo#isNewUser()
which is on AdditionalUserInfo
, available on AuthResult
when the task resolves.
Upvotes: 0
Reputation: 317552
You're better off defining for yourself what it means to be a "new user", and using a database to reflect that. For example, if a user doesn't have any data in your database keyed by their UID, you could make the assumption that they are new. Then you update the database to reflect the fact that they are no longer new.
Upvotes: 2