注销并登录后重新初始化用户信息 [英] Users info are re-initialized after logging out and signing in
本文介绍了注销并登录后重新初始化用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当用户注销并再次尝试登录时,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()
方法,如下所示:
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屋!
查看全文