When I log in with a different user Session is populated with from the last 
Session and not the current User, should I use $cacheFactory?

angular.module('services.auth', ['services.session'])
  .factory('AuthService', ['apiUrl', '$http', '$localStorage', 'Session', 
'$q', '$timeout',
    function (apiUrl, $http, $localStorage, Session, $q, $timeout) {
      return {
        login: function (credentials) {
          var deferredOne = $q.defer();
          var promiseOne = deferredOne.promise;
          $http
            .post(apiUrl + '/authentication', credentials)
              .success(function(data){
                $localStorage.token = data.token;
                deferredOne.resolve(data);
              })
              .error(function(err){
                deferredOne.reject(err);
              });

          var deferredTwo = $q.defer();
          var promiseTwo = deferredTwo.promise;

          $http.get(apiUrl + '/users/me')
          .success(function(data){
            deferredTwo.resolve(data);
          })
          .error(function(err){
            deferredTwo.reject(err);
          });
           
          return $q.all([promiseOne, promiseTwo])
            .then(function(promises){
              var user = promises[1];
                Session.create(user.id, user.firstName);
             // console.log(Session.firstName);
            });
        }, 
        logout: function(){
          $localStorage.$reset();
          Session.destroy();
        },
         isAuthenticated: function () {
          return !!Session.userId;
        },
         isAuthorized: function (authorizedRoles) {
          if (!angular.isArray(authorizedRoles)) {
            authorizedRoles = [authorizedRoles];
          }
          return (this.isAuthenticated() &&
            authorizedRoles.indexOf(Session.userRole) !== -1);
          } 
      };
  }]);

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to