all files / services/ auth.factory.js

81.48% Statements 22/27
33.33% Branches 2/6
85.71% Functions 6/7
81.48% Lines 22/27
4 statements, 1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59              119×   119×       119×     119×                         95×     119×     119×     119× 85×     119×     119×    
/*global angular*/
 
angular.module('webmapp')
 
    .factory('Auth', function Auth(
        $rootScope,
        MapService
    ) {
        var auth = {};
 
        var userData = {},
            isLoggedIn = false;
 
        /* istanbul ignore next */
        Iif (localStorage.user) {
            userData = JSON.parse(localStorage.user);
            isLoggedIn = true;
            MapService.setItemInLocalStorage("$wm_userData", JSON.stringify(userData));
            delete localStorage.user;
        }
 
        MapService.getItemFromLocalStorage("$wm_userData")
            .then(function (data) {
                if (data.data && typeof data.data === 'string') {
                    userData = JSON.parse(data.data);
                    isLoggedIn = true;
                    $rootScope.$emit('logged-in');
                }
                else {
                    isLoggedIn = false;
                }
            })
            .catch(function (err) {
                // console.warn("$wm_userData: " + err.message);
                isLoggedIn = false;
            });
 
        auth.setUserData = function (value) {
            userData = value;
            MapService.setItemInLocalStorage("$wm_userData", JSON.stringify(value));
            isLoggedIn = true;
        };
 
        auth.resetUserData = function () {
            MapService.removeItemFromLocalStorage("$wm_userData");
            isLoggedIn = false;
        };
 
        auth.getUserData = function () {
            return userData;
        };
 
        auth.isLoggedIn = function () {
            return isLoggedIn;
        };
 
        return auth;
    });