4

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)
    }}
}
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
JDOEs
  • 41
  • 1
  • 1
  • 2
  • 1
    Possible duplicate of [Firebase: How to keep an Android user logged in?](https://stackoverflow.com/questions/22262463/firebase-how-to-keep-an-android-user-logged-in) – Manohar Nov 14 '17 at 12:33

2 Answers2

8

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.

....

Philip Herbert
  • 4,599
  • 2
  • 37
  • 40
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
  • How can I make sure the user is logged in? – JDOEs Nov 14 '17 at 12:37
  • 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 – JDOEs Nov 14 '17 at 12:43
  • 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 Nov 14 '17 at 12:45
  • No, you will still be logged in - [even if the user is deleted, your app will still hold an authentication token](https://stackoverflow.com/a/35961217/2754146), so is technically still authenticated. To logout, clear the app data or use [`FirebaseAuth#signOut()`](https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.html#signOut()). – Grimthorr Nov 14 '17 at 12:47
  • You can clear app data on the Android device through: `Settings > Applications > Your App > Clear Data`, or use [`adb shell pm clear packageName`](https://stackoverflow.com/questions/10934304/clear-android-application-user-data) where `packageName` is the full package name of your app. – Grimthorr Nov 14 '17 at 12:50
  • Anyway apart from signet or that to check/incheck that token? – JDOEs Nov 14 '17 at 12:51
  • The only way to clear the authentication session (and logout the current user) is to clear the app data or call [`FirebaseAuth.getInstance().signOut()`](https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.html#signOut()). – Grimthorr Nov 14 '17 at 12:58
0

If anyone landing up here for achieving same thing using Java then use following code (credit to Grimthorr's answer for the Kotlin version that this is a port of)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    auth = FirebaseAuth.getInstance();

    if (auth.getCurrentUser() != null) {
        // User is signed in (getCurrentUser() will be null if not signed in)
        Intent intent = Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
        // or do some other stuff that you want to do
    }
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122