Search code examples
androidsessionsharedpreferencessession-state

Android Storing User Session in Shared Preferences


I want to create a User Session on Android so that i do not have to login every time. What content should be stored in Shared Preferences so that i can authenticate every time my server gets a request from the user i can make sure people are not hacking into my system.

The users can login via the following in my app

  1. Facebook
  2. Google

Do i need to convert and store some encrypted data in Shared Preferences ?

Or just Storing the users Email or Username should be enough.


Solution

  • Its easy to store the credential in shared preferences So that when you splash screen comes it you can check it and redirect the user to the next screen without asking user to Login into google or facebook.

    I have used the preferences to login using facebook and our own server. For that i hae stored one boolean variable that user is is login with facebook or our own server then if the user loged in with our own server then we have called the webservice in background with stored usercreadential in preferences and if user loged in with facebook then we have usered

    if (Application.prefs.isFacebookLogin()) {
            facebook = new Facebook(Application.APP_ID);
            // Instantiate the asynrunner object for asynchronous api calls.
            SessionStore.restore(facebook);
            SessionEvents.addAuthListener(new FbAPIsAuthListener());
            if (facebook.isSessionValid()) {
                Application.prefs.setAccessTokenFb(facebook
                        .getAccessToken());
                Application.prefs.setExpirationFB(facebook
                        .getAccessExpires());
            }
            // redirectHome();
            // finish();
        }
    

    Here after that we have redirect user to the first screen if the creadential goes right.