2015-06-29 22:51:29 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('users').factory('Principal', ['$window', '$http', '$q', '$timeout', '$state',
|
|
|
|
function($window, $http, $q, $timeout, $state) {
|
|
|
|
|
2015-06-30 07:28:29 +00:00
|
|
|
var service = {
|
|
|
|
_currentUser: null,
|
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
isIdentityResolved: function() {
|
2015-06-30 07:28:29 +00:00
|
|
|
if(service._currentUser === null) return false
|
|
|
|
return true;
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
|
|
|
isAuthenticated: function() {
|
2015-06-30 07:28:29 +00:00
|
|
|
return !!service._currentUser;
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
|
|
|
isInRole: function(role) {
|
2015-06-30 07:28:29 +00:00
|
|
|
if (!service.isAuthenticated() || !service._currentUser.roles) return false;
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-06-30 07:28:29 +00:00
|
|
|
return service._currentUser.roles.indexOf(role) !== -1;
|
2015-06-29 22:51:29 +00:00
|
|
|
},
|
|
|
|
isInAnyRole: function(roles) {
|
2015-06-30 07:28:29 +00:00
|
|
|
if (!service.isAuthenticated() || !service._currentUser.roles) return false;
|
2015-06-29 22:51:29 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < roles.length; i++) {
|
|
|
|
if (this.isInRole(roles[i])) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
authenticate: function(user) {
|
2015-06-30 07:28:29 +00:00
|
|
|
service._currentUser = user;
|
|
|
|
|
|
|
|
// store the user in $window
|
2015-06-29 22:51:29 +00:00
|
|
|
if (user) $window.user = user;
|
|
|
|
else $window.user = null;
|
|
|
|
},
|
|
|
|
signin: function(credentials) {
|
|
|
|
|
|
|
|
var deferred = $q.defer();
|
|
|
|
$http.post('/auth/signin', credentials).success(function(response) {
|
2015-06-30 07:28:29 +00:00
|
|
|
console.log(response);
|
2015-06-29 22:51:29 +00:00
|
|
|
// If successful we assign the response to the global user model
|
2015-06-30 07:28:29 +00:00
|
|
|
service.authenticate(response);
|
2015-06-29 22:51:29 +00:00
|
|
|
deferred.resolve(response);
|
2015-06-30 07:28:29 +00:00
|
|
|
}).error(function(error) {
|
|
|
|
|
|
|
|
deferred.reject(error.message || error);
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
signup: function(credentials) {
|
|
|
|
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
$http.post('/auth/signup', credentials).success(function(response) {
|
|
|
|
// If successful we assign the response to the global user model
|
|
|
|
deferred.resolve(response);
|
2015-06-30 07:28:29 +00:00
|
|
|
}).error(function(error) {
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-06-30 07:28:29 +00:00
|
|
|
deferred.reject(error.message || error);
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
signout: function() {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
$http.get('/auth/signout').success(function(response) {
|
|
|
|
// If successful we assign the response to the global user model
|
2015-06-30 07:28:29 +00:00
|
|
|
deferred.resolve(null);
|
|
|
|
service.authenticate(null);
|
|
|
|
}).error(function(error) {
|
|
|
|
deferred.reject(error.message || error);
|
2015-06-29 22:51:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
2015-06-30 07:28:29 +00:00
|
|
|
identity: function() {
|
2015-06-29 22:51:29 +00:00
|
|
|
|
2015-06-30 07:28:29 +00:00
|
|
|
if (service.isAuthenticated()) {
|
|
|
|
return service._currentUser;
|
|
|
|
} else if($window.user){
|
|
|
|
service.authenticate($window.user);
|
|
|
|
return service._currentUser;
|
2015-06-29 22:51:29 +00:00
|
|
|
}else {
|
2015-06-30 07:28:29 +00:00
|
|
|
return $http.get('/user/me')
|
|
|
|
.success(function(response) {
|
|
|
|
service.authenticate(response.data.user);
|
|
|
|
return response.data.user;
|
|
|
|
})
|
|
|
|
.error(function() {
|
|
|
|
service.authenticate(null);
|
|
|
|
// $state.go('signin');
|
|
|
|
return null;
|
|
|
|
});
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
2015-06-30 07:28:29 +00:00
|
|
|
|
|
|
|
// var deferred = $q.defer();
|
|
|
|
|
|
|
|
// console.log($window.user);
|
|
|
|
// console.log(service.isAuthenticated());
|
|
|
|
|
|
|
|
// // check and see if we have retrieved the user data from the server. if we have, reuse it by immediately resolving
|
|
|
|
// if (service.isAuthenticated() === true ) {
|
|
|
|
// deferred.resolve(service.currentUser);
|
|
|
|
// }else if($window.user){
|
|
|
|
|
|
|
|
// service.authenticate($window.user);
|
|
|
|
// deferred.resolve(service._currentUser);
|
|
|
|
// }else {
|
|
|
|
|
|
|
|
// // otherwise, retrieve the user data from the server, update the user object, and then resolve.
|
|
|
|
// $http.get('/users/me')
|
|
|
|
// .success(function(response) {
|
|
|
|
// service.authenticate(response);
|
|
|
|
// deferred.resolve(response);
|
|
|
|
// })
|
|
|
|
// .error(function() {
|
|
|
|
// service.authenticate(null);
|
|
|
|
// deferred.reject("User's session has expired");
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return deferred.promise;
|
|
|
|
|
2015-06-29 22:51:29 +00:00
|
|
|
}
|
|
|
|
};
|
2015-06-30 07:28:29 +00:00
|
|
|
|
|
|
|
return service;
|
2015-06-29 22:51:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
]);
|