First, your use of promise chains is way too complicated. Here is a 
simplified version:

function login(credentials)
{
  return *authenticate*( credentials )
              .then( *cacheToLocal*  )
              .then( *loadMe*        )
              .then( *createSession* );

    // Step (1)
    function *authenticate*( credentials )
    {
      return $http.post( apiUrl + '/authentication', credentials );
    }

    // Step (2)
    function *cacheToLocal*( data )
    {
      $localStorage.token = data.token;
      return data;
    }

    // Step (3)
    function *loadMe*( data ) 
    {
      return $http.get(apiUrl + '/users/me')
                  .then( function onResponse(me)
                  {
                      // me data is currently ignored... !!
                     * return data;*
                  });
    }

    // Step (4)
    function *createSession*( user )
    {
      Session.create(user.id, user.firstName);
    }
}



On Monday, July 7, 2014 10:14:14 PM UTC-5, Olajide Ogundipe Jr wrote:
>
> 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