注销和登录后,用户信息将重新初始化 [英] Users info are re-initialized after logging out and signing in

查看:166
本文介绍了注销和登录后,用户信息将重新初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户注销并尝试再次登录时, JSON 树中的所有属性都会重新初始化。

Whenever the user is logging out and tries to log in once more all the attributes in the JSON tree are re-initialized.

这是我的代码:

 @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RC_SIGN_IN) {
                IdpResponse response = IdpResponse.fromResultIntent(data);
                if (resultCode == RESULT_OK) {
                    // Sign-in succeeded, set up the UI
                    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                    FirebaseUserMetadata metadata =mFirebaseAuth.getCurrentUser().getMetadata();
                    if (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp()) {
                        // The user is new, show them a fancy intro screen!
                        User user = new User(firebaseUser.getUid(), firebaseUser.getDisplayName(), firebaseUser.getEmail());

                       mDatabaseReference.child(user.getId()).setValue(user);
                    } else {
                        // This is an existing user, show them a welcome back screen.
                        Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                        startActivity(intent);
                    }
                } else if (resultCode == RESULT_CANCELED) {
                    // Sign in was canceled by the user, finish the activity
                    Toast.makeText(this, "Sign in canceled", Toast.LENGTH_SHORT).show();
                    finish();
                }

我试图使用用户的元数据,但即使这样也无效。

I tried to use user's metadata but even that didn't work.

这是我的 JSON 结构

推荐答案

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

If we want to check if a user is new, it's the same thing if we check if the user logs in for the first time. 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", "Is New User!");
            } else {
                Log.d("TAG", "Is Old User!");
            }
        }
    }
};

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

For more informations, please see the official doc.

这篇关于注销和登录后,用户信息将重新初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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