android - 如何检查用户是否在 Firebase 中通过身份验证?

标签 android firebase firebase-authentication google-signin

我的应用程序中有几种注册方法,如下所示:

enter image description here

让我们围绕 Google 登录方法解决问题:
我正在做的是当用户登录时 enteringDataIntoUserNode() 方法在 signInWithCredential() 方法中被调用,例如:

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    AuthCredential credential = 
           GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    //  signing in
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {

                        //  entering default values in the database
                        enteringDataIntoUserNode();

                        Intent intent = new Intent(LoginAndSignupActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    else {
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                    }

                }
            });
}

enteringDataIntoUserNode()

private void enteringDataIntoUserNode() {
    final String currentUid = mAuth.getCurrentUser().getUid();

    //
    String deviceToken = FirebaseInstanceId.getInstance().getToken();
    final String userName = mAuth.getCurrentUser().getDisplayName();
    String imageUrl = mAuth.getCurrentUser().getPhotoUrl().toString();
    String userStatus = "Hi there! How is it going?";

    //  inserting data in key-value pair
    usersReference.child(currentUid).child("user_name").setValue(userName);
    usersReference.child(currentUid).child("user_status").setValue(userStatus);
    usersReference.child(currentUid).child("user_image").setValue(imageUrl);
    usersReference.child(currentUid).child("device_token").setValue(deviceToken);

    //  when this last value will be inserted then, making an intent to MainActivity
    usersReference.child(currentUid).child("user_thumb_image").setValue(imageUrl)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        Log.d(TAG, "onComplete: Data insertion for the new user is successful ");
                    }
                }
            });
}

enteringDataIntoUserNode() 方法只是在用户每次登录时将一些默认数据传递到数据库。但是如果用户更改其详细信息(如图像或用户名等)并注销并再次登录通过 Google 登录方法,然后再次调用 enteringDataIntoUserNode() 方法,用户详细信息将被默认信息覆盖。

我的问题是,在 Google 登录 期间,是否有任何方法可以检查用户是否是第一次登录,以便在后一种情况下我可以跳过调用 enteringDataIntoUserNode() 方法并防止覆盖数据。 在 FacebookPhone Number 登录期间,我想实现同样的目标。

最佳答案

如果你检查用户是否是第一次登录,如果你想检查一个用户是否是新用户也是一样的。所以要解决这个问题,我们可以简单地调用 OnCompleteListener.onComplete 回调中的 isNewUser() 方法,如下所示:

OnCompleteListener<AuthResult> completeListener = new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            boolean isNewUser = task.getResult().getAdditionalUserInfo().isNewUser();
            if (isNewUser) {
                Log.d("TAG", "The user is new!");
            } else {
                Log.d("TAG", "The user is not new!");
            }
        }
    }
};

更多信息,请查看 official documentation .

关于android - 如何检查用户是否在 Firebase 中通过身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51443538/

相关文章:

android - 如何截取屏幕截图?

android - 如何创建包含 = 符号的 firebase 路径(不转换为 %3D)

swift - 密码重置 swift 4 firebase

javascript - 如何在经过 firebase 身份验证的用户后从服务器端重定向浏览器

java - 使用 FirestorePagingAdapter 的 SnapshotParser

Firebase 帐户创建限制

java - 代码风格建议放入 "Copyright Statement"

android - 远程服务和偏好 Activity

android - 如何定义翻转动画

java - 如何获取用户id以在android studio中执行显示列表以供管理员查看订单列表?