【发布时间】:2019-09-21 12:36:10
【问题描述】:
您好,感谢您的宝贵时间。
我一直在使用 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() 与 signInWithEmailAndPassword() 方法分开,这使我在两个活动 onCreate() 方法中添加了这个 mAuth = FirebaseAuth.getInstance()。在我添加的登录活动中
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();
}
【问题讨论】:
-
您之前成功退出了吗?
-
没有试过,但如果我清除应用程序缓存和东西,它会断开用户的连接。只是不会检测到他不再存在。
-
-
嗯...我完全错过了。可能是我换东西的时候!我会看看它是怎么回事,但非常感谢你的所有帮助!!! @JeelVankhede
-
乐于助人:)
标签: android firebase authentication firebase-authentication