How to keep session in multipage angular application?

前端 未结 1 803
无人及你
无人及你 2021-02-06 17:01

I am having single page application with user authentication and there is no problem sharing session information there.

However I have part of site where are static pag

相关标签:
1条回答
  • 2021-02-06 17:09

    I would recommend creating a service that wraps localStorage or other apis to store persistent data. Here is an example using a localStorage implementation.

    This implementation is synchronous but if I would use websql like or even server db then I would refactor it to use promises to return the storage object.

    Controller

    var demo = angular.module('demo', [ 'appStorage' ]);
    
    demo.controller('AppStorageController', [ '$scope', 'appStorage',
        function($scope, appStorage) {
          appStorage('MyAppStorage', 'myAppStorage', $scope);
        } ]);
    

    HTML

    <div ng-controller="AppStorageController">
      <p>Local Storage: {{myAppStorage}}</p>
      <p>
        Username: <input type="text" ng-model="myAppStorage.username"></input>
      </p>
      <p>
        Remember me: <input type="checkbox"
          ng-model="myAppStorage.rememberMe"></input>
      </p>
    </div>
    

    JS

    angular.module('appStorage', []).factory('appStorage',
        [ '$window', function($window) {
          var appStorages = {};
          var api = undefined;
    
          if ($window.localStorage) {
            api = {
              set : function(name, value) {
                $window.localStorage.setItem(name, JSON.stringify(value));
              },
              get : function(name) {
                var str = $window.localStorage.getItem(name);
                var val = {};
                try {
                  val = str ? JSON.parse(str) : {};
                }
                catch (e) {
                  console.log('Parse error for localStorage ' + name);
                }
                return val;
              },
              clear : function() {
                $window.localStorage.clear();
              }
            };
          }
          // possibly support other
    
          if (!api) {
            throw new Error('Could not find suitable storage');
          }
    
          return function(appName, property, scope) {
            if (appName === undefined) {
              throw new Error('appName is required');
            }
    
            var appStorage = appStorages[appName];
    
            var update = function() {
              api.set(appName, appStorage);
            };
    
            var clear = function() {
              api.clear(appName);
            };
    
            if (!appStorage) {
              appStorage = api.get(appName);
              appStorages[appName] = appStorage;
              update();
            }
    
            var bind = function(property, scope) {
              scope[property] = appStorage;
              scope.$watch(property, function() {
                update();
              }, true);
            };
    
            if (property !== undefined && scope !== undefined) {
              bind(property, scope);
            }
    
            return {
              get : function(name) {
                return appStorage[name];
              },
              set : function(name, value) {
                appStorage[name] = value;
                update();
              },
              clear : clear
            };
          };
        } ]);
    
    0 讨论(0)
提交回复
热议问题