Question

I am writing a mobile app using AngularJS and would like to store some data locally. Specifically a boolean on if the user is logged in and some details on the user that was fetched from LDAP.

For instance, here's the code for checking if the user is logged in:

angular.module('starter.services', ['ionic'])

.factory('AuthService', function() {

    var userAuthenticated = false;

    this.setUserAuthenticated = function(value) {
        userAuthenticated = value;
    };

    this.getUserAuthenticated = function() {
        return userAuthenticated;
    };

    return this;
})

I'm not sure what the lifetime of a service variable is? It's important to not have to log in (for a reasonable period of time) after the initial login. Ex. I turn my phone off and when I turn it off, the variables should still persist.

Will there ever be a case when I open up the app and userAuthenticated will reset itself to false when I want it to be true?

It seems to be that way when I run it on the browser with Cordova, but the opposite when I compile Cordova onto the iOS simulator.

I'm thinking that if using a service variable won't persist long enough, I should store the variable value into local storage?

Thanks

Was it helpful?

Solution

A service variable will last as long as the memory is kept alive. If the user somehow refresh the page (if possible) or force kill the app / restart phone you'll loss everything and this is normal.

As you guess, you'll have to store it somehow persistently.

You're first choice here would obviously be localStorage. You also have a few angular modules that helps handle that like https://github.com/gsklee/ngStorage

You could also use cookies (a bit deprecated for me but if you need to handle expiracy, why not) or a database (websql)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top