Android Firebase Auth getCurrentUser()检索已删除的用户
你好,谢谢你抽出时间 我一直在使用Firebase开发android应用程序 我已设置Firebase身份验证,用户可以使用电子邮件和密码注册,并在电子邮件验证后登录 当应用程序打开时,我会检查用户是否仍然使用Android Firebase Auth getCurrentUser()检索已删除的用户,android,firebase,authentication,firebase-authentication,Android,Firebase,Authentication,Firebase Authentication,你好,谢谢你抽出时间 我一直在使用Firebase开发android应用程序 我已设置Firebase身份验证,用户可以使用电子邮件和密码注册,并在电子邮件验证后登录 当应用程序打开时,我会检查用户是否仍然使用onStart()方法登录,但在从firebase中删除用户后,我尝试了这一点,我仍然可以登录 我应该用另一种方式检查吗 @Override public void onStart() { super.onStart(); // Check if user is sign
onStart()
方法登录,但在从firebase中删除用户后,我尝试了这一点,我仍然可以登录
我应该用另一种方式检查吗
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
*******************************************更新********************************************* 使用修复了此问题 ,但随后我将signIn和createAccount方法拆分为两个独立的活动,并将
createAccount()
与signInWithEmailAndPassword()方法分开,这使我在这两个活动onCreate()
方法中添加了此mAuth=FirebaseAuth.getInstance()
。在我添加的登录活动中
mAuthListener=new FirebaseAuth.AuthStateListener(){
@凌驾
AuthStateChanged上的公共void(@NonNull FirebaseAuth FirebaseAuth){
FirebaseUser currentUser=mAuth.getCurrentUser();
...
}
};代码>
但现在不行了。我是忘了什么还是就是做不到
以下是我发现的相关代码:
LogInActivity类onCreate():
符号活动类onCreate():
似乎firebase在本地保存用户首选项尝试清除应用程序并再次登录检查此文档您可以收听了解firebase用户的状态更改
为什么?基本上,当您从服务器或其他设备中删除用户时,它在当前设备中仍然有效,因为它的令牌尚未在本地刷新
了解更多信息
文件指出:
在某些情况下,getCurrentUser
将返回一个非nullFirebaseUser
,但基础令牌无效。
例如,如果用户在另一个设备上被删除,而本地令牌尚未刷新,则可能发生这种情况。
在这种情况下,您可能会获得一个有效的用户getCurrentUser
,但随后对经过身份验证的资源的调用将失败
getCurrentUser
也可能返回null,因为auth对象尚未完成初始化
如果附加一个AuthStateListener
,则每次基础令牌状态更改时,都会得到一个回调。这有助于对上述边缘情况做出反应
你以前成功注销过吗?还没有尝试过,但是如果我清除应用缓存之类的东西,它将断开用户连接。只是检测不到他已经不存在了。@PedroMarques你忘了添加监听器,比如addAuthStateListener(AuthStateListener)
,然后在onDestroy()
中删除它,比如removeAuthStateListener(AuthStateListener)
来注册或取消注册监听器。嗯……我完全错过了。可能是我把事情改变的时候!我会看看进展如何,但非常感谢你的帮助@Jeelvankheedhappy要帮忙:)非常感谢!我会加上它,看看它是否解决了问题!我用你的答案解决了这个问题,然后决定将登录和注册分为两个活动。在此基础上,我还将createAccount()与signInWithEmailAndPassword()方法分开,这使我在两个activity onCreate()方法中都添加了“mAuth=FirebaseAuth.getInstance()”。在登录活动中,我添加了mAuthListener=new FirebaseAuth.AuthStateListener(){@Override public void onAuthStateChanged(@NonNull FirebaseAuth FirebaseAuth){FirebaseUser currentUser=mAuth.getCurrentUser();…};但现在不行了。我是忘了什么还是就是做不到?你能在OP中分享代码吗?我会调查一下,然后再打给你?对不起,我没能赶到那里
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
// Views
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.pswdEditText);
// Buttons
findViewById(R.id.logInButton).setOnClickListener(this);
findViewById(R.id.forgottenPaswdTextButton).setOnClickListener(this);
findViewById(R.id.registerTextButton).setOnClickListener(this);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// Check for user connection
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
Log.d(TAG, "onAuthStateChanged:signed_in:" + currentUser.getUid());
} else {
Log.d(TAG, "onAuthStateChanged:signed_out");
}
updateUI(currentUser);
}
};
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
// Views
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.pswdEditText);
passwordRetypedEditText = findViewById(R.id.pswdRetypeEditText);
nameEditText = findViewById(R.id.nameEditText);
// Buttons
findViewById(R.id.signUpButton).setOnClickListener(this);
findViewById(R.id.logInTextButton).setOnClickListener(this);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}