Firebase Android Auto Login

12,069

Firebase Authentication does automatically remember authentication state, so the user will still be authenticated when the app is restarted.

However, if your LoginActivity is the launcher activity, you'll still land on this activity, so you'll need to check whether the user is authenticated in onCreate(), and then redirect them to your MainActivity if they are already logged in, something like:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    auth = FirebaseAuth.getInstance();

    if (auth.getCurrentUser() != null) {
        // User is signed in (getCurrentUser() will be null if not signed in)
        val intent = Intent(this, MainActivity::class.java);
        startActivity(intent);
        finish();
    }
}

This makes use of the FirebaseAuth#getCurrentUser() method that will return a FirebaseUser object if the user is logged in, or null if they are not logged in.

Alternatively, you could swap it around so that the MainActivity is the launcher activity and then only show your LoginActivity if the user is not logged in.

....

Share:
12,069
JDOEs
Author by

JDOEs

Updated on June 09, 2022

Comments

  • JDOEs
    JDOEs about 2 years

    So I setup email/password register and login.

    That is working. I thought Firebase took care of this but apparently not. I want, after the user closes the app, to be logged in already next time they open the app.

    What is missing?

    class LoginActivity : AppCompatActivity(){
        lateinit var auth: FirebaseAuth
        lateinit var user: FirebaseAuth
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_login)
    
            auth = FirebaseAuth.getInstance()
        }
    
        fun loginLoginClicked(view: View) {
            // Perform login
    
            val email = loginEmailTxt.text.toString()
            val password = loginPasswordTxt.text.toString()
    
            auth.signInWithEmailAndPassword(email, password)
                    .addOnSuccessListener {
                        finish()
                    }
                    .addOnFailureListener { exception ->
                        Log.e("Exception", "Could not sign in user - ${exception.localizedMessage}")
                    }
            val loginIntent = Intent(this, MainActivity::class.java)
            startActivity(loginIntent)
        }
    
        fun loginCreateClicked(view: View) {
            // segue to the create user activity
    
            val createIntent = Intent(this, SignUpActivity::class.java)
            startActivity(createIntent)
        }}
    }
    
  • JDOEs
    JDOEs over 6 years
    How can I make sure the user is logged in?
  • JDOEs
    JDOEs over 6 years
    Because I deleted all the users, and the code you gave me still takes me to the MainActivity. And there are no users so it is impossible to be logged in
  • Grimthorr
    Grimthorr over 6 years
    Like I've shown in my example: auth.getCurrentUser() will be null if the user is not logged in. If the user is logged in, the auth.getCurrentUser() will return a FirebaseUser object.
  • Grimthorr
    Grimthorr over 6 years
    No, you will still be logged in - even if the user is deleted, your app will still hold an authentication token, so is technically still authenticated. To logout, clear the app data or use FirebaseAuth#signOut().
  • Grimthorr
    Grimthorr over 6 years
    You can clear app data on the Android device through: Settings > Applications > Your App > Clear Data, or use adb shell pm clear packageName where packageName is the full package name of your app.
  • JDOEs
    JDOEs over 6 years
    Anyway apart from signet or that to check/incheck that token?
  • Grimthorr
    Grimthorr over 6 years
    The only way to clear the authentication session (and logout the current user) is to clear the app data or call FirebaseAuth.getInstance().signOut().