如何在Firebase中检查用户是否已通过身份验证? [英] How to check whether a user is authenticated or not in Firebase?

查看:107
本文介绍了如何在Firebase中检查用户是否已通过身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a few sign-up methods in my app as shown below:

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

Let's revolve the problem around Google sign-in method:
What I am doing is that when a user signs in enteringDataIntoUserNode() method is called inside signInWithCredential() method like:

    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()方法,并且默认用户名将覆盖用户详细信息.

enteringDataIntoUserNode() method is simply passing some default data to the database every time a user signs in. But if a user change its details like image or username etc. and sign out and again sign in through Google sign in method then again enteringDataIntoUserNode() method will be called and user details would be overwritten with the default ones.

我的问题是,有什么方法可以检查用户是否在 Google登录期间是第一次登录,这样在后一种情况下,我可以跳过呼叫 enterDataIntoUserNode()方法,并防止数据被覆盖. 在 Facebook 电话号码登录期间,我也想实现相同的目的.

My question is that is there any way to check whether a user is signing in for the first time or not during the Google sign in so that in the latter case I can skip calling enteringDataIntoUserNode() method and prevent overwriting of data. Same thing I want to acheive during Facebook and Phone Number sign in.

推荐答案

如果您检查用户是否首次登录,则要检查用户是否是新用户也是一样.因此,要解决此问题,我们可以像这样在OnCompleteListener.onComplete回调中简单地调用isNewUser()方法:

If you check if the user logs in for the first time, it's the same thing if you want to check if a user is new. So to solve this, we can simply call the isNewUser() method in the OnCompleteListener.onComplete callback like this:

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!");
            }
        }
    }
};

有关更多信息,请参见官方文档.

For more informations, please see the official documentation.

这篇关于如何在Firebase中检查用户是否已通过身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆