Skip to content

Instantly share code, notes, and snippets.

@martianyi
Last active March 21, 2017 02:23
Show Gist options
  • Save martianyi/d2f22099883fd537ec6d to your computer and use it in GitHub Desktop.
Save martianyi/d2f22099883fd537ec6d to your computer and use it in GitHub Desktop.
angularjs security service
var securityService = angular.module('security.service', []);
//Security
securityService.factory('Security', [
'$http', '$location', '$cookieStore', 'API_SERVER', '$window', '$route', 'toaster',
function ($http, $location, $cookieStore, API_SERVER, $window, $route, toaster) {
var security = {};
//login
security.login = function (credentials) {
return $http.post(API_SERVER + 'login/', credentials).success(
//sucess
function (data) {
$cookieStore.put('token', data.key);
$cookieStore.put('user', data.user);
var user = $cookieStore.get('user');
if (user.is_superuser == true) {
$location.path('/backend');
$window.location.reload();
}
else if (user.is_superuser == false) {
$location.path('/');
$route.reload();
}
}
).error(
function () {
toaster.pop('error', '', 'wrong username or password')
}
)
};
//logout
security.logout = function () {
$cookieStore.remove('token');
$cookieStore.remove('user');
$location.path('/');
$route.reload();
};
return security;
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment