Firebase Android Auto Login
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.
....
JDOEs
Updated on June 09, 2022Comments
-
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 over 6 yearsHow can I make sure the user is logged in?
-
JDOEs over 6 yearsBecause 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 over 6 yearsLike I've shown in my example:
auth.getCurrentUser()
will benull
if the user is not logged in. If the user is logged in, theauth.getCurrentUser()
will return aFirebaseUser
object. -
Grimthorr over 6 yearsNo, 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 over 6 yearsYou can clear app data on the Android device through:
Settings > Applications > Your App > Clear Data
, or useadb shell pm clear packageName
wherepackageName
is the full package name of your app. -
JDOEs over 6 yearsAnyway apart from signet or that to check/incheck that token?
-
Grimthorr over 6 yearsThe only way to clear the authentication session (and logout the current user) is to clear the app data or call
FirebaseAuth.getInstance().signOut()
.