Skip to content

Instantly share code, notes, and snippets.

@fnakstad
Last active December 19, 2015 23:09
Show Gist options
  • Save fnakstad/6032902 to your computer and use it in GitHub Desktop.
Save fnakstad/6032902 to your computer and use it in GitHub Desktop.
angular.module('angular-client-side-auth')
.factory('Auth', function($http, $rootScope, $cookieStore){
// ...
$rootScope.accessLevels = accessLevels;
$rootScope.userRoles = userRoles;
return {
authorize: function(accessLevel, role) {
if(role === undefined)
role = $rootScope.user.role;
return accessLevel & role;
},
isLoggedIn: function(user) {
if(user === undefined)
user = $rootScope.user;
return user.role === userRoles.user || user.role === userRoles.admin;
},
register: function(user, success, error) {
$http.post('/register', user).success(success).error(error);
},
login: function(user, success, error) {
$http.post('/login', user).success(function(user){
$rootScope.user = user;
success(user);
}).error(error);
},
logout: function(success, error) {
$http.post('/logout').success(function(){
$rootScope.user = {
username = '',
role = userRoles.public
};
success();
}).error(error);
},
accessLevels: accessLevels,
userRoles: userRoles
};
});
@arvidkahl
Copy link

FYI, line 36 and 37 do not correctly set object properties, should be : instead of =

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment