android - Firebase Auth getCurrentUser() 检索已删除的用户
问题描述
您好,感谢您的宝贵时间。
我一直在使用 Firebase 开发一个 android 应用程序。
我已经设置了 Firebase 身份验证,用户可以使用电子邮件和密码注册并在电子邮件验证后登录。
当应用程序打开时,我检查用户是否仍然使用该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);
}
******************************************* 更新 ****** ******************************************
使用AuthStateListener修复了问题
,但随后我将 signIn 和 createAccount 方法拆分为 2 个单独的活动,这样我还分离了createAccount()
fromsignInWithEmailAndPassword()
方法,这使我mAuth = FirebaseAuth.getInstance()
在两个活动onCreate()
方法中都添加了这个。在我添加的登录活动中
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser currentUser = mAuth.getCurrentUser();
...
}
};
但现在不起作用。我是忘记了什么还是无法做到这一点?
这是我发现相关的代码:
LogInActivity 类 onCreate():
@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);
}
};
}
SignInActivity 类 onCreate():
@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();
}
解决方案
您可以收听AuthStateListener的FirebaseUser
状态变化。
为什么?,基本上当您从服务器或其他设备中删除用户时,它在您当前的设备中仍然有效,因为它的令牌尚未在本地刷新。
在此处了解更多信息。
文档指出:
在某些情况下,
getCurrentUser
将返回非 nullFirebaseUser
但基础令牌无效。例如,如果用户在另一台设备上被删除并且本地令牌未刷新,则可能会发生这种情况。在这种情况下,您可能会获得一个有效用户getCurrentUser
,但随后对经过身份验证的资源的调用将失败。
getCurrentUser
也可能返回null,因为 auth 对象尚未完成初始化。如果您附加一个,则每次底层令牌状态更改时
AuthStateListener
都会收到回调。这对于对上述边缘情况做出反应很有用。
推荐阅读
- c++ - 保存地图
到文本文件 - rsync - rsync 间歇性地失败,在同一命令上出现不同的错误代码
- pyspark - ParquetDecodingException 使用 pyspark
- bash - crontab 不执行脚本和日志文件不工作
- python - 使用 Python Elasticsearch Client 为空索引添加索引模式
- ios - 将两个字典转换为一个字典作为 Swift 中的键值对
- javascript - 使用primefaces在xhtml页面中打印条形码时如何删除页眉和页脚
- signalr-hub - Azure SignalR 服务测试
- javascript - 如何转换mysql查询的数组结果以在chartist.js中使用
- c# - RelativeSource 不使用子属性工作