1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-28 13:36:41 -05:00
shimapan/app/public/services/AuthSvc.js

61 lines
1.9 KiB
JavaScript
Executable File

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 !== 401)
$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 === 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);
});
}
}]);