1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-15 01:06:32 -05:00
shimapan/app/public/services/AuthSvc.js
2018-01-15 17:13:35 -05:00

61 lines
1.9 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)
if (obj.hasOwnProperty(p))
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: user
}).then(function(res) {
if (res.status === 200)
$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)
if (obj.hasOwnProperty(p))
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: user
}).then(function(res) {
if (res.status === 200)
$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);
});
}
}]);