Grégoire Sion Grégoire Sion - 4 months ago 20
AngularJS Question

how to store login info with angularJS

I am currently develloping a little app with angularJS;
the users have to go through a login form and then they can click on a few links who display data fetched from a server.

The login form only takes the user's input and it is then "stored" into a factory

app.factory('IdFormHolder', function () {
var that = this;

that.setId = function (data) {
that = data;

};
that.getId = function () {
return that;
};

return that;
});


Which works fine I need to keep it stored because the server requires the login form to be sent each time I do a $http.get as a header.

Each controller takes the login form from the factory and uses it to get the data from the server.

This works fine until someone decides to refresh the page, at which point it seems the factory is "emptied" from its login form, and the web-app then fails to show anything.

Is there a way to store this login info so that it doesn't get erased so easily ?

Answer

You can use this code after youve installed sessionStorage:

app.factory('IdFormHolder', function ($sessionStorage) {
    that = this;

    that.setId = function (data) {
        $sessionStorage.id = data;
        that = data;

    };
    that.getId = function () {
        return $sessionStorage.id;
    };

    return that;
});

Download Link: https://github.com/gsklee/ngStorage

Comments