【问题标题】:How to check whether a user is authenticated or not in Firebase?如何检查用户是否在 Firebase 中通过身份验证?
【发布时间】:2018-12-28 19:47:32
【问题描述】:

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

让我们围绕 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());
                    }

                }
            });
}

进入DataIntoUserNode()

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 登录期间检查用户是否是第一次登录,以便在后一种情况下我可以跳过调用 进入DataIntoUserNode() 方法并防止数据被覆盖。 我希望在 Facebook电话号码 登录期间实现同样的目标。

【问题讨论】:

    标签: android firebase firebase-authentication google-signin


    【解决方案1】:

    如果您检查用户是否是第一次登录,如果您要检查用户是否是新用户,则相同。所以为了解决这个问题,我们可以像这样在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

    【讨论】:

    • 非常感谢,亚历克斯。
    【解决方案2】:

    您可以检查帐户的创建时间:

    task.getResult().getUser().getMetadata().getCreationTimestamp()
    

    请参阅reference docs

    但一种常见的方法也是检查用户是否已经存在于数据库using a transaction中。

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2017-09-11
      • 2018-11-11
      • 2017-10-15
      相关资源
      最近更新 更多