代码之家  ›  专栏  ›  技术社区  ›  shreyansh

页面刷新导致用户在angular js中自动注销

  •  8
  • shreyansh  · 技术社区  · 8 年前

    我有一个登录API,我正在我的服务中使用它

    function logInToService(callback, errback, login, password, rememberLogin) {
                var url = "User/Login";
    
                var authorizationHeader = {'Authorization': "Basic " + login + ":" + password};
                httpWrapperService.post(url, {login: login, password: password}, authorizationHeader).then(
                    function success(loginToken) {
                        // transform data here
    
                        self.currentUser.emailAddress = login;
                        self.currentUser.password = password;
                        // store token in a cookie
                        self.currentUser.token = loginToken;
                        $rootScope.currentUser = self.currentUser;
    
    
                        if (rememberLogin) {
    
                            localStorage.userName=login;
                            localStorage.password=password;
                        }
    
                        httpWrapperService.setAuthenticationToken(loginToken);
                        callback(loginToken);
                    },
                    function error(errorObject) {
                        errback(errorObject);
                    }
                );
            }
    

    在我的标题html中,当他登录时,我会在顶部显示用户名 标题.html

     <div class="header-user-menu">
            <div ng-show="isUserLoggedIn() == false ">
                <a href="" ng-click="$state.go('app.sr.login')" class="">{{'HEADER.LOGIN' | translate}}</a>
                <!--<a ui-sref="app.sr.login" class="">{{'HEADER.LOGIN' | translate}}</a>-->
            </div>
            <div ng-show="isUserLoggedIn()" class="userName">{{'HEADER.WELCOME' | translate}} {{currentUser.emailAddress}}</div>
    
        </div>
    

    js文件在这里

    (function() {
        'use strict';
    
        angular
            .module('safe-repository')
            .controller('AppLayoutCtrl', appLayoutCtrl);
    
        // Implementation of controller 
        appLayoutCtrl.$inject = ['$rootScope', '$scope'];
    
        function appLayoutCtrl($rootScope, $scope) {
    
    
            $scope.isUserLoggedIn = isUserLoggedIn;
    
            function isUserLoggedIn() {
                if ($rootScope.currentUser
                    && $rootScope.currentUser.emailAddress != null
                    && $rootScope.currentUser.emailAddress != '') {
                    return true;
                }
                return false;
            }
    
        }
    })();
    

    这里我有一个注册服务,我定义了 logInToService 方法

    function registrationService($rootScope, httpWrapperService, $modal, $state, $cookieStore) {
            var self = this;
            // expose functions
            self.hasUserAccessToLevel = hasUserAccessToLevel;
            self.logInToService = logInToService;
            self.getCurrentUserToken = getCurrentUserToken;
            self.showRegistrationViewForLevel = showRegistrationViewForLevel;
            self.currentUser = {
                //emailAddress: '',
                //password: '',
                //token: ''
            }
    
            $rootScope.currentUser = null;
            self.currentUserToken = null;
    
     function logInToservice (------){----}})();
    

    问题是,每次用户按下页面刷新F5时,用户都会注销。尽管我试图将数据存储在本地存储中,但实际上我没有得到控制流。

    2 回复  |  直到 8 年前
        1
  •  7
  •   Borys Serebrov    8 年前

    您正在尝试将数据保存到localStorage,但没有读取它。 这个 currentUser 用户数据所在的变量是一个常规javascript变量,当您重新加载页面时,该变量会被重置。

    你需要这样做:

    // ...
    // user logs in, remember it into the local storage
    // Note: is is better to use it via $window.localStorage
    $window.localStorage.setItem('user', JSON.stringify(user));
    this.currentUser = user;
    
    //...
    // function to get the user, if this.currentUser is not set,
    // try to load from the local storage
    getUser: function() {
      if (this.currentUser) {
          return this.currentUser;
      }
      var storageUser = $window.localStorage.getItem('user');
      if (storageUser) {
        try {
          this.user = JSON.parse(storageUser);
        } catch (e) {
          $window.localStorage.removeItem('user');
        }
      }
      return this.currentUser;
    }
    
    // you may also want to remove the user data from storage when he logs out
    logout: function() {
        $window.localStorage.removeItem('user');
        this.currentUser = null;
    },
    
        2
  •  0
  •   Ankush Rishi    8 年前

    您需要创建一个登录会话,以便在页面刷新后不会注销。看看这个链接:

    http://maffrigby.com/maintaining-session-info-in-angularjs-when-you-refresh-the-page/