Home > Blockchain >  How can I still get the email address when I enabled skip login on the second launch of the app in a
How can I still get the email address when I enabled skip login on the second launch of the app in a

Time:11-15

I enabled the skip login, wherein I am not able to get the email of the account I was logged in to.

How can I still get the email if skip login is enabled in the second launch of the app?

CodePudding user response:

The Firebase authentication state persists across application restarts. So there is no need to save the email address locally in order to use it again when you restart the application. As soon as you create an instance of the FirebaseAuth class, you can call getCurrentUser() which:

Returns the currently signed-in FirebaseUser or null if there is none.

As you can see, the type object that is returned is FirebaseUser. If this object is null it means that your user is not logged in, otherwise, you can call FirebaseUser#getEmail(). In this way, you can always get the email address from this object, without the need to perform any other operations.

If you want to track the auth state, then I recommend you check my answer from the following post:

If you want to see a real example, please check this resource, with the corresponding repo.

CodePudding user response:

While launching the app first time, after login you can save the username and email in Shared Preference and on second time launch you can fetch it. First initialize App Preference using method

AppPreferences.INSTANCE.initAppPreferences(ActivityName.this);

Then set userName and use it.

Code is shared below

public enum AppPreferences {
INSTANCE;
private static final String SHARED_PREFERENCE_NAME = "AppName";
private SharedPreferences mPreferences;
private Editor mEditor;


public void initAppPreferences(Context context) {
    mPreferences = context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
    mEditor = mPreferences.edit();
}

public String getUserName() {
    return mPreferences.getString(SharedPreferencesKeys.user_name.toString(), "");
}

public void setUserName(String value) {
    mEditor.putString(SharedPreferencesKeys.user_name.toString(), value);
    mEditor.commit();
}
}
  • Related