I'm working on an app that uses Firebase authentication to sign in users through phone number. I want to add a functionality such that there is only one-time login for the user, i.e. even if the user kills the app and starts it again, he should be logged in. Also, I don't want to add a logout feature. What can be done for this?
3 Answers
The simplest way to achieve this is to use a listener. Let's assume you have two activities, the LoginActivity
and the MainActivity
. The listener that can be created in the LoginActivity
should look like this:
FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
};
This basically means that if the user is logged in, skip the LoginActivity
and go to the MainActivity
.
Instantiate the FirebaseAuth
object:
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
And start listening for changes in your onStart()
method like this:
@Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(authStateListener);
}
In the MainActivity
, you should do the same thing:
FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser == null) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
}
};
Which basically means that if the user is not logged in, skip the MainActivity
and go to the LoginActivity
. In this activity you should do the same thing as in the LoginActivity
, you should start listening for changes in the onStart()
.
In both activities, don't forget to remove the listener in the moment in which is not needed anymore. So add the following line of code in your onStop()
method:
@Override
protected void onStop() {
super.onStop();
firebaseAuth.removeAuthStateListener(authStateListener);
}

- 565,676
- 79
- 828
- 807

- 130,605
- 17
- 163
- 193
-
Yes, it worked! But I think shared preferences too would be good to use. – Einzig7 Jun 19 '18 at 12:32
-
1It would be if don't care about app uninstall. `SharedPreferences` do not persist across app uninstall while Firebase keeps authentication data in a back-up even if the application is deleted. So in this case, if you uninstall the app, there will be no loss of informations. It's up to you to decide which solution is better for you. – Alex Mamo Jun 19 '18 at 12:43
-
Thanks a lot, Alex! Actually I'm making an app for visually impaired people, and using now I realize that using authStateListener will be the right decision. – Einzig7 Jun 19 '18 at 12:54
-
I have tried your method but after uninstalling the app, the app still requires the user to authenticate. What went wrong, any idea? – Einzig7 Jun 23 '18 at 10:10
-
Is the cache set to true in the AndroidManifest.xml file? – Alex Mamo Jun 23 '18 at 17:28
-
How do you set that? I couldn't find any such feature there. However, `android:allowBackup` was already set to `true`. – Einzig7 Jun 23 '18 at 18:11
-
This `android:fullBackupContent="true"` should also be set to `true`. – Alex Mamo Jun 25 '18 at 07:39
-
Are you sure you are not signing out somehow? – Alex Mamo Jun 25 '18 at 08:04
-
And you haven't added other sign-in method like google, right? – Alex Mamo Jun 25 '18 at 08:28
-
No, I haven't. Authentication is only through the phone number. – Einzig7 Jun 25 '18 at 08:32
-
Without seeing your code, I cannot tel you more that the Firebase authentication session is persisted on the user's device in the Android back-up, as explained above. This data for the application is not removed when the application is uninstalled. – Alex Mamo Jun 25 '18 at 08:52
You can save user login session in shared Preference
Do this when your login got Login Success
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", Activity.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.commit();
and When Your apps is Starts (like Splash or login Page) use this
SharedPreferences sp=this.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
boolean b = sp.getBoolean("key_name", false);
if(b==true){
//User Already Logged in skip login page and continue to next process
}else{
//User is Not logged in, show login Form
}
It will works for you.

- 1,888
- 2
- 9
- 20
-
-
It is the preference Name, i forget to change it ,now i updated answer @Einzig7 – Sandeep Parish Jun 16 '18 at 08:28
-
Also, I'm getting an error here for the getBoolean function, it says that getBoolean has two parameters, a string and a boolean. – Einzig7 Jun 16 '18 at 08:33
-
-
This is from my working code from logging class:
private void LoginUserAccount(String email, String password)
{
if (TextUtils.isEmpty(email))
{
Toast.makeText(ResponseStartPage.this, "Please write Your Email", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(password))
{
Toast.makeText(ResponseStartPage.this, "Please write Your password", Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle("Login Account");
loadingBar.setMessage("Please wait for verification...");
loadingBar.show();
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if (task.isSuccessful())
{
String online_user_id = mAuth.getCurrentUser().getUid();
String DeviceToken = FirebaseInstanceId.getInstance().getToken();
usersReference.child(online_user_id).child("device_token").setValue(DeviceToken)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid)
{
Intent mainIntent = new Intent(ResponseStartPage.this, ResponseMainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
});
}
else
{
Toast.makeText(ResponseStartPage.this, "Please Check your email and password", Toast.LENGTH_SHORT).show();
}
loadingBar.dismiss();
}
});
}
}

- 45
- 9