检查用户是否第一次使用gmail登录

时间:2018-04-05 04:55:08

标签: android firebase firebase-authentication google-signin

您好我想检查登录我的应用程序的用户是否是第一次使用

task.getResult().getAdditionalUserInfo().isNewUser()

但如果始终返回false,即使我使用的帐户也从未登录过我的帐户。

这些是我的一些代码。

private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .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
                        isNew = task.getResult().getAdditionalUserInfo().isNewUser();
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        //updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText( NewLoginActivity.this, "Authentication Failed.", Toast.LENGTH_SHORT).show();
                        //updateUI(null);
                    }


                }
            });
}

我想检查用户是否登录,因为如果用户从未登录我的应用程序,我希望用户填写一些信息。

mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            Log.d(TAG, String.valueOf(isNew));
            if ( mAuth.getCurrentUser() != null && isNew ){         // New User
                Log.d(TAG, "New User");
                startActivity( new Intent(NewLoginActivity.this, NewRegisterForSocialActivity.class) );
            }
            else if ( mAuth.getCurrentUser() != null && !isNew ) {  // Old User
                startActivity( new Intent(NewLoginActivity.this, MainActivity.class) );
            }
        }
    };

0 个答案:

没有答案