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

61 lines
1.9 KiB
JavaScript
Raw Normal View History

var angular = require('angular');
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)
2018-01-13 17:36:40 -05:00
if (obj.hasOwnProperty(p))
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
2017-10-11 10:15:19 -04:00
return str.join("&");
},
data: user
}).then(function(res) {
2018-01-13 19:06:52 -05:00
if (res.status !== 401)
$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)
2018-01-13 17:36:40 -05:00
if (obj.hasOwnProperty(p))
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
2017-10-11 10:15:19 -04:00
return str.join("&");
},
data: user
}).then(function(res) {
2017-10-18 13:31:08 -04:00
if (res.status === 401) return false;
$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
}]);