问题
How can I validate user is already signed up?
private void firebaseAuthWithGoogle(final GoogleSignInAccount acct, final GoogleSignInResult result) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//HOW CAN I validate user is already signup or not
if (Singup == true) {
did not create account.
} else {
create account;
}
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(Login.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
// updateUI(null);
}
}
});
}
This is creating account when user already exists or not every time.
回答1:
You will need to have an instance of FirebaseAuth
. Then within if (task.isSuccessful()) {...}
block, get an instance of FirebaseUser
using FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
.
After which you can replace
if (Singup == true) {
// did not create account.
} else {
// create account;
}
with
if (firebaseUser == null) {
// did not create account.
} else {
// create account;
}
More details can be found on lines 137 and 197 sample and here (Firebase google sigin doc)
来源:https://stackoverflow.com/questions/46800611/how-can-i-validate-user-is-already-signed-up