2016-02-02 13 views
5

ho un API di login, che Io lo utilizzo nel mio servizioaggiornamento della pagina causando utente per ottenere il log out automaticamente in js angolari

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); 
       } 
      ); 
     } 

E nel mio header HTML io sono la visualizzazione del nome utente in alto quando lui viene loggato header.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> 

e il file js è qui

(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; 
     } 

    } 
})(); 

e qui ho un servizio di registrazione in cui ho definito logInToService metodo

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 (------){----}})(); 

Il problema è che ogni volta che la pagina di aggiornamento utente preme F5, l'utente ottiene disconnesso. Anche se sto cercando di memorizzare i dati in localstorage, ma in realtà non sto ottenendo il flusso del controllo.

risposta

6

Si sta tentando di salvare i dati in localStorage, ma non si sta leggendo. La variabile currentUser in cui sono inseriti i dati utente è una normale variabile javascript che viene ripristinata quando si ricarica la pagina.

Hai bisogno di fare qualcosa di simile:

// ... 
// 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; 
}, 
Problemi correlati