0

I'm trying to determine if the user is new or an existing one. When user is registered, e-mail verification is required. When verification is successful, the user can sign in for the first time. When a new user signing in, he must fill a mandatory form with his details, so it's a completely different activity.

I've tried to use use the getAdditionalUserInfo().isNewUser() but it's always returns false. I'm not sure if the e-mail verification somehow messes it up

mAuth.signInWithEmailAndPassword(mEmail, mPassword)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            // Sign in success, update UI with the signed-in user's information
            FirebaseUser user = mAuth.getCurrentUser();
            updateUI(user);
            boolean flag = task.getResult().getAdditionalUserInfo().isNewUser();
            if (flag) {
                Toast.makeText(EmailPasswordActivity.this, "NEW USER.",
                        Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(EmailPasswordActivity.this, "EXISTING USER.",
                        Toast.LENGTH_SHORT).show();

            }
        } else {
            // If sign in fails, display a message to the user.
            Log.w(TAG, "signInWithEmail:failure", task.getException());
            Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
                    Toast.LENGTH_SHORT).show();
            updateUI(null);
        }

        // [START_EXCLUDE]
        if (!task.isSuccessful()) {
            mStatusTextView.setText(R.string.auth_failed);
        }
        hideProgressDialog();
        // [END_EXCLUDE]
    }
});

1 Answers1

1

You could either

A) Save a boolean underneath their user profile in your firebase database to determine that they're a new user you can toggle it to true once they've signed in for the first time.

B) Or to save an un-needed web call, you could locally store a boolean in their shared preferences, to do the same thing, and set that to true after the first time they have signed in.

Joshua Best
  • 246
  • 1
  • 14