0

I wrote a simple authentification:

       auth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        //if login is succesfull open mainActivity
                        if(task.isSuccessful()){
                            Intent intentMainActivity = new Intent(LoginPage.this, MainActivity.class);
                            startActivity(intentMainActivity);

                    }else {
                            Toast.makeText(LoginPage.this, "Failure: "+task.getException(), Toast.LENGTH_LONG).show();

                        } 

When the app starts, the onStart-Method checks if the user is logged in or not:

@Override
public void onStart(){
    super.onStart();
    //Checks if the user is loged in or not, If not go to loginpage

     FirebaseUser currentUser = auth.getCurrentUser();
     if(currentUser == null){
         Intent intentLoginPage = new Intent(MainActivity.this, LoginPage.class);
         startActivity(intentLoginPage);
     }


}

When the user logs in he jumps back to the login screen. I did it exactly as the Firebase documentation showed. I think the user is still null after the login. Due to that it jumps back to the login screen. What can I do to solve this issue?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
KayraOz
  • 31
  • 5
  • My best guess is that your `onStart` runs before Firebase has restored the authentication state (which requires an asynchronous call to the server). If that is the cause, the solution is to use an auth state listener as shown here: https://stackoverflow.com/collectives/google-cloud/articles/68104924/listen-for-authentication-state-in-android – Frank van Puffelen Sep 18 '22 at 20:10
  • Frank van is right. thats the proper way. – Jeet Chhatrala Sep 19 '22 at 06:55
  • I think this [answer](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth) will also help. – Alex Mamo Sep 19 '22 at 08:47

0 Answers0