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