如何防止用户在使用 SharedPreferences 和 Firebase 身份验证关闭应用程序时被要求重新登录

How to prevent users from being asked to re-login when closing the app using SharedPreferences and Firebase Authentication

我仍在学习 Firebase。我使用 Firebase 身份验证创建了一个登录程序。在使 LoginActivity 和 HomeActivity 程序 运行 顺利进行时,当我按下返回按钮然后再次打开应用程序时,要求用户重新登录。如何让用户不被要求重新登录?

我试图在 Whosebug 和 YouTube 上的多个频道上找到相同问题的解决方案,但没有找到答案。非常感谢任何答案,如果我的英语很乱,我很抱歉。

这是来自 LoginActivity 的代码片段。

FirebaseAuth mAuth = FirebaseAuth.getInstance();

SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);

mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        progressBar.setVisibility(View.GONE);
                        if (task.isSuccessful()){
                            Toast.makeText(LoginActivity.this, "Login succes !", Toast.LENGTH_SHORT).show();
                            user = mAuth.getCurrentUser();
                            updateUI(user);
                        }else{
                            Toast.makeText(LoginActivity.this, "Login Gagal !", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


private void updateUI(FirebaseUser user) {
        if (user != null){
            String id = user.getUid();
            String userEmail = user.getEmail();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("firebaseKey", id);
            editor.commit();

            gotoHomeActivity();
        }
    }

private void gotoHomeActivity() {
        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

        finish();
    }

这是来自 HomeActivity 的代码片段。

FirebaseAuth mAuth = FirebaseAuth.getInstance();

 SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);


String uid = sharedPreferences.getString("firebaseKey", "");

一旦您的用户登录,他们在使用 FirebaseAuth 时通常不需要再次登录。问题可能是您每次都在调用“登录”例程。

相反,您应该首先在登录屏幕上添加一个身份验证状态侦听器,当您在那里收到响应时,您可以转到主屏幕(如果他们已经登录)或启动登录过程.

mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser() != null) {
                // already logged in, go to home screen
            }
            else {
                // initiate sign-in
            }
        }
    }
);

请注意,一旦您注册了侦听器,就会立即调用侦听器,有时 again shortly thereafter。如果这最终成为一个问题,那么在链接的问题中有一些解决方案。

Kotlin 版本

auth.addAuthStateListener(AuthStateListener { firebaseAuth ->
    if( firebaseAuth.currentUser != null ) {
        // user is already logged in, go to home screen
    }
    else {
        // initiate sign in
    }
})

在您的 LoginActivity 中,检查用户是否已经登录-

if(mAuth.getCurrentUser()!=null){

// if true open MainActivity
            Intent intent = new Intent(SignupActivity.this,MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); // to stop flashing of activity when intent is working
            startActivity(intent);
            finish();
        }