Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Firebase中检查用户是否经过身份验证?_Android_Firebase_Firebase Authentication_Google Signin - Fatal编程技术网

Android 如何在Firebase中检查用户是否经过身份验证?

Android 如何在Firebase中检查用户是否经过身份验证?,android,firebase,firebase-authentication,google-signin,Android,Firebase,Firebase Authentication,Google Signin,我的应用程序中有几种注册方法,如下所示: 让我们围绕谷歌登录方法来解决这个问题: 我所做的是,当用户登录enteringDataIntoUserNode()方法时,会在signInWithCredential()方法中调用该方法,如: private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { AuthCredential credential = GoogleAuthProvider.

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

让我们围绕谷歌登录方法来解决这个问题:
我所做的是,当用户登录
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());
                    }

                }
            });
}
private void firebaseAuthWithGoogle(谷歌签名帐户){
AuthCredential凭据=
GoogleAuthProvider.getCredential(acct.getIdToken(),null);
//登录
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//在数据库中输入默认值
输入DataInToUserNode();
意向意向=新意向(LoginAndSignupActivity.this,MainActivity.class);
星触觉(意向);
完成();
}
否则{
w(标记“signInWithCredential:failure”,task.getException());
}
}
});
}
输入DataInToUserNode()

private void enteringDataIntoUserNode(){
最后一个字符串currentUid=mAuth.getCurrentUser().getUid();
//
字符串deviceToken=FirebaseInstanceId.getInstance().getToken();
最终字符串userName=mAuth.getCurrentUser().getDisplayName();
字符串imageUrl=mAuth.getCurrentUser().getPhotoUrl().toString();
String userStatus=“你好!进展如何?”;
//在键值对中插入数据
usersReference.child(currentUid).child(“用户名”).setValue(用户名);
usersReference.child(currentUid).child(“用户状态”).setValue(用户状态);
usersReference.child(currentUid).child(“用户图像”).setValue(图像URL);
usersReference.child(currentUid).child(“设备令牌”).setValue(deviceToken);
//当最后一个值将被插入时,表示要进行MainActivity
usersReference.child(currentUid).child(“user\u thumb\u image”).setValue(imageUrl)
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Log.d(标记“onComplete:新用户的数据插入成功”);
}
}
});
}
enteringDataIntoUserNode()方法只是在用户每次登录时向数据库传递一些默认数据。但是,如果用户更改其详细信息,如图像或用户名等,然后注销,然后通过Google登录方法再次登录,那么将再次调用enteringDataIntoUserNode()方法,并且用户详细信息将被默认值覆盖

我的问题是,是否有任何方法可以检查用户是否在Google登录期间首次登录,以便在后一种情况下,我可以跳过调用enteringDataIntoUserNode()方法并防止数据被覆盖。
在Facebook和电话号码登录过程中,我也想实现同样的目标。

您可以在创建帐户时使用以下工具进行检查:

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


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

如果您检查用户是否首次登录,那么如果您想检查用户是否是新用户也是一样的。因此,为了解决这个问题,我们可以简单地调用
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!");
            }
        }
    }
};

OnCompleteListener.

非常感谢,亚历克斯。
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!");
            }
        }
    }
};