1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-10 23:53:31 -05:00
shimapan/app/public/services/AuthSvc.js
2017-10-20 16:46:14 -04:00

59 lines
1.8 KiB
JavaScript

var angular = require('angular');
angular.module('AuthSvc', []).service('AuthService', ['$http', '$window', function($http, $window) {
this.login = function(user) {
return $http({
method: 'POST',
url: '/api/auth/login',
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) {
if (res.status === 401) return false;
$window.location.href = '/home';
})
};
this.logout = function() {
$http({
method: 'GET',
url: '/api/auth/logout'
}).then(function() {
$window.location.href = '/';
});
};
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) {
if (res.status === 401) return false;
$window.location.href = '/home';
});
};
this.currentUser = function(cb) {
return $http({
method: 'GET',
url: '/api/auth/session',
headers: {'Content-Type': 'application/json'}
}).then(function(res) {
cb(res.data);
});
}
}]);