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
According to the new version of Firebase auth (16.0.1) The AuthResult class has a member function which results true or false (is the user is new). Assuming "credential" is defined in the scope(it is the google credential ). An example is shown below: `
private FirebaseAuth mAuth;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mAuth = FirebaseAuth.getInstance();
Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
GoogleSignInAccount acct = task.getResult(ApiException.class);
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Log.d(TAG, "linkWithCredential:success");
boolean newuser = task.getResult().getAdditionalUserInfo().isNewUser();
if(newuser){
//Do Stuffs for new user
}else{
//Continue with Sign up
}
} else {
Toast.makeText(MyClass.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
});
Thanks to firebase:)