How to Keep Android Applications Always Be Logged in State

How to keep android applications always be logged in state?

Use Shared Preference for auto login functionality. When users log in to your application, store the login status into sharedPreference and clear sharedPreference when users log out.

Check every time when the user enters into the application if user status from shared Preference is true then no need to log in again otherwise direct to the login page.

To achieve this first create a class, in this class you need to write all the function regarding the get and set value in the sharedpreference. Please look at this below Code.

public class SaveSharedPreference 
{
static final String PREF_USER_NAME= "username";

static SharedPreferences getSharedPreferences(Context ctx) {
return PreferenceManager.getDefaultSharedPreferences(ctx);
}

public static void setUserName(Context ctx, String userName)
{
Editor editor = getSharedPreferences(ctx).edit();
editor.putString(PREF_USER_NAME, userName);
editor.commit();
}

public static String getUserName(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
}
}

Now in the main activity (The "Activity" where users will be redirected when logged in) first check

if(SaveSharedPreference.getUserName(MainActivity.this).length() == 0)
{
// call Login Activity
}
else
{
// Stay at the current activity.
}

In Login activity if user login successful then set UserName using setUserName() function.

How to keep android applications always be login state when application get force update from play store?

First, create Preferences.java (helper):

package ru.str.proglot.visitor.helpers;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class Preferences {

@SuppressWarnings("unchecked")
public static <T> T getValue(Context context, String key, T defaultValue) {
SharedPreferences sPref = PreferenceManager.getDefaultSharedPreferences(context);
T resultValue = null;

if (defaultValue instanceof Boolean)
resultValue = (T) Boolean.valueOf(sPref.getBoolean(key, (Boolean) defaultValue));
else if (defaultValue instanceof Float)
resultValue = (T) Float.valueOf(sPref.getFloat(key, (Float) defaultValue));
else if (defaultValue instanceof Integer)
resultValue = (T) Integer.valueOf(sPref.getInt(key, (Integer) defaultValue));
else if (defaultValue instanceof Long)
resultValue = (T) Long.valueOf(sPref.getLong(key, (Long) defaultValue));
else if (defaultValue instanceof String)
resultValue = (T) sPref.getString(key, (String) defaultValue);

return resultValue;
}

public static <T> void setValue(Context context, String key, T value) {
SharedPreferences sPref = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sPref.edit();

if (value instanceof Boolean)
editor.putBoolean(key, (Boolean) value);
else if (value instanceof Float)
editor.putFloat(key, (Float) value);
else if (value instanceof Integer)
editor.putInt(key, (Integer) value);
else if (value instanceof Long)
editor.putLong(key, (Long) value);
else if (value instanceof String)
editor.putString(key, (String) value);

editor.apply();
}

public static void remove(Context context, String key) {
PreferenceManager.getDefaultSharedPreferences(context).edit().remove(key).apply();
}

public static void clear(Context context ) {
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply();
}

}

Next, save info from anywhere:

Preferences.setValue(this, "name", "Ivan");

And get value:

Preferences.getValue(this, "name", "");

How to keep user Logged-In always Connected with Server

Storing user's credentials on device is not a good way of designing. You can store the Hash password, which is also denied as good application design technique. According to the facebook and google these tech giants use Authentication token login-logout. Once the user log in server generate token for particular user which is then stored on your device as well as the server. Next time user come to App a request has been made to check the token is valid or not, if valid - access granted else not.

A basic design of this process

Sample Image

Tutorial :

  • Look for basic tutorials
  • Facebook login

My app stayed logged in even after restart

@Sebastian makes a good point saying that you need to use the AND operator in the if statement instead of the OR operator. That's because it checks if the email exists or if the password exists. You as you posted only removed the password, so the email is still there. The program takes it as signed in. Also, instead of independently deleting the password and email, just use:

sharedPreferences.clear() //Clears every single value

That would also help with the removal of cache files and save memory space that is wasted.

Edit:
The error actually is in your second snippet of code. This code to get the shared preferences:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

This does get the default shared preferences, but instead of getting the preferences where you save your email and your password, it returns a whole different set of shared preferences. What you should do instead is this:

SharedPreferences pref = getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);

It's the same as you do in MainActivity.java and that was the error: it needed to be the same. Here String PREF_NAME = "sp_name";. The preference name/ids, they exist for this exact thing to identify shared preferences.

If you want to learn more about shared preferences and how they work look at the documentation: https://developer.android.com/reference/android/content/SharedPreferences

How to keep a user persistent logged in through my Android application with Firebase

When a user of your app authenticates with Firebase, the Firebase client stores the token in the shared preferences on the user's Android device. So when the app restarts, the user is already authenticated (unless the token has expired).

So normally you'd only call Login() (or show a "Log in" button) when the onAuthStateChanged() gets null for its authData.

Keeping a user logged in

Firebase Authentication can automatically persist the user's authentication state, and restore it when the app is restarted/page is reloaded. But on some environments this functionality is not enabled by default, and you have to configure it yourself as shown in the documentation on auth state persistence. This should tak the form of something like:

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)

After that your onAuthStateChanged should start working.

Also see:

  • Persistence of firebase auth on Ionic 4 App on the Ionic forums
  • these results of searching for firebase auth persistennce ionic.


Related Topics



Leave a reply



Submit