Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app login

There are some apps that require login. May I know how the app manage user session on android? For example, once the user has sign in to the app, the next time user start the app, it will straight go to the home page instead of the login page. While if user start the app for the first time or before login, the app will start with the login page. Can phonegap handle this? Thank you.

like image 955
davidlee Avatar asked Sep 02 '26 11:09

davidlee


1 Answers

Depending on which version of Android you are targeting and which version of PhoneGap you are running, you should use one of the built-in offline storage mechanisms available in the browser. These include localStorage and WebKitSQLite.

There is also a fantastic open source library that you can use that abstracts any specific offline storage mechanism and allows for swappable underlying storage adapters, and provides a single unified key/value interface. The library is called Lawnchair - check it out!

So on-load, you would instantiate Lawnchair and see if your saved user parameters exist:

function onLoad() {
    myStore = new Lawnchair();
    myStore.get('login', function(i) {
        if (i == null) {
            // user did not login before, no saved credentials.
        } else {
            // user DID login, we can now auto-login for the user.
        }
    });
}

Additionally, following a successful manual login, you can save the credentials to Lawnchair so that they will be available next time your PhoneGap app loads and checks for their existence:

function login(username, password) {
    /* 
     * Do the login stuff here
     */
    if (/* login was successful */) {
        myStore.save({key:'login',value:{username:username, password:password}});
    } else {
        alert('Could not log you in!');
    }
}

The most important bit between the two chunks of code you see here is the first parameter to the get() function, and the key property to the object passed into the save() function. Both of these need to match to retrieve the same object.

Hope that helps!

like image 193
fil maj Avatar answered Sep 04 '26 01:09

fil maj