2017-10-11 10:15:19 -04:00
|
|
|
angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', function($http, $window) {
|
|
|
|
this.login = function(user) {
|
|
|
|
return $http({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/api/auth/login',
|
2017-10-11 13:11:57 -04:00
|
|
|
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
2017-10-11 10:15:19 -04:00
|
|
|
transformRequest: function(obj) {
|
|
|
|
var str = [];
|
|
|
|
for (var p in obj)
|
|
|
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
|
|
|
|
return str.join("&");
|
|
|
|
},
|
|
|
|
data: user
|
|
|
|
}).then(function(res) {
|
2017-10-18 13:31:08 -04:00
|
|
|
if (res.status === 401) return false;
|
2017-10-14 16:31:41 -04:00
|
|
|
$window.location.href = '/home';
|
2017-10-11 10:15:19 -04:00
|
|
|
})
|
|
|
|
};
|
2017-10-18 13:31:08 -04:00
|
|
|
|
2017-10-11 10:15:19 -04:00
|
|
|
this.logout = function() {
|
2017-10-14 17:49:11 -04:00
|
|
|
$http({
|
|
|
|
method: 'GET',
|
|
|
|
url: '/api/auth/logout'
|
2017-10-18 13:31:08 -04:00
|
|
|
}).then(function() {
|
2017-10-14 17:49:11 -04:00
|
|
|
$window.location.href = '/';
|
|
|
|
});
|
2017-10-11 10:15:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.register = function(user) {
|
|
|
|
return $http({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/api/auth/register',
|
|
|
|
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
|
|
|
transformRequest: function(obj) {
|
|
|
|
var str = [];
|
|
|
|
for (var p in obj)
|
|
|
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
|
|
|
|
return str.join("&");
|
|
|
|
},
|
|
|
|
data: user
|
|
|
|
}).then(function(res) {
|
2017-10-18 13:31:08 -04:00
|
|
|
if (res.status === 401) return false;
|
2017-10-14 16:31:41 -04:00
|
|
|
$window.location.href = '/home';
|
2017-10-11 10:15:19 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-18 13:31:08 -04:00
|
|
|
this.currentUser = function(cb) {
|
|
|
|
return $http({
|
|
|
|
method: 'GET',
|
|
|
|
url: '/api/auth/session',
|
|
|
|
headers: {'Content-Type': 'application/json'}
|
|
|
|
}).then(function(res) {
|
|
|
|
cb(res.data);
|
|
|
|
});
|
2017-10-14 15:16:58 -04:00
|
|
|
}
|
2017-10-11 10:15:19 -04:00
|
|
|
}]);
|