Created
December 12, 2016 06:54
-
-
Save pavtaras/57305778f8f9ab5c13f38665d398a7eb to your computer and use it in GitHub Desktop.
Angular 1 authentication
This file contains 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
angular.module('mod').service('PagePermissionService', function(CurrentUser, $rootScope, $state){ | |
this.start = function(){ | |
$rootScope.$on('$stateChangeStart', stateChangeListener); | |
}; | |
function stateChangeListener(event, toState, toParams) { | |
document.title = toState.data.siteTitle; | |
var roles = toState.data.roles; | |
if(CurrentUser.isAuthenticated()){ | |
try{ | |
CurrentUser.authorizeSync(roles); | |
} catch(e){ | |
event.preventDefault(); | |
catchAuthError(e); | |
} | |
} else { | |
event.preventDefault(); | |
CurrentUser.authorize(roles) | |
.then(function(){ | |
$state.go(toState.name, toParams); | |
}) | |
.catch(catchAuthError); | |
} | |
} | |
function catchAuthError(error){ | |
console.info(error.message); | |
if(error instanceof UserNotFoundError){ | |
location.href = "/login"; | |
} else if(error instanceof LackOfPermissionsError){ | |
location.href = "/login"; | |
} | |
} | |
}); |
This file contains 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
angular.module('mod').factory('CurrentUser', function($q, $timeout, ApiRequest){ | |
var _identity = null, | |
deferred = null; | |
return { | |
isAuthenticated: function(){ | |
return !!_identity; | |
}, | |
getIdentity: function(){ | |
return _identity; | |
}, | |
setIdentity: function(identity) { | |
_identity = identity; | |
}, | |
setAvatar: function(avatarUrl){ | |
if(_identity){ | |
_identity.avatar = avatarUrl; | |
} | |
}, | |
getAvatar: function(){ | |
return _identity && _identity.avatar; | |
}, | |
clear: function(){ | |
_identity = null; | |
deferred = null; | |
}, | |
isAdmin: function(){ | |
return !!_identity && _identity.role === 'admin'; | |
}, | |
logout: function(){ | |
return ApiRequest.post('auth/logout') | |
.then(function(response){ | |
localStorage.removeItem("sessionId"); | |
location.href = "/login"; | |
return response; | |
}); | |
}, | |
getUser: function(){ | |
var self = this; | |
if(deferred !== null){ | |
return deferred.promise; | |
} | |
deferred = $q.defer(); | |
if(this.isAuthenticated()){ | |
deferred.resolve(_identity); | |
return deferred.promise; | |
} | |
ApiRequest.get('auth/identity').then(function(data){ | |
var user = data.user; | |
user.role = user.role.name; | |
self.setIdentity(user); | |
deferred.resolve(_identity); | |
deferred = null; | |
console.info("User was successfully fetched", _identity); | |
}, function(error){ | |
deffered.reject(new UserNotFoundError("User wasn't fetched")); | |
}); | |
return deferred.promise; | |
}, | |
authorize: function(roles){ | |
var self = this; | |
return this.getUser() | |
.then(function(){ | |
return self.authorizeSync(roles); | |
}); | |
}, | |
authorizeSync: function(roles){ | |
roles = roles || []; | |
if(!_identity){ | |
throw new UserNotFoundError("User wasn't found"); | |
} | |
if(roles.indexOf(_identity.role) === -1){ | |
throw new LackOfPermissionsError("You don't have enough permissions"); | |
} | |
return true; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment