0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ahsan Saeed
  • 41
  • 1
  • 2
  • 9
  • Check this post: https://stackoverflow.com/questions/43030237/firebase-check-email-registered-if-login-via-facebook-and-google You can track if a user is new or existing using Firebase Database. You would have to build that mechanism. – bojeil Oct 19 '17 at 08:28

1 Answers1

0

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)

OliverTester
  • 401
  • 2
  • 7