Last active
March 21, 2017 02:23
-
-
Save martianyi/d2f22099883fd537ec6d to your computer and use it in GitHub Desktop.
angularjs security service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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