Chapter 7
To prevent us from having to repeat ourselves, we can actually put this array
into the provider for the authorization service, as given in the following code:
.provider('securityAuthorization', {
requireAdminUser: [
'securityAuthorization',
function(securityAuthorization) {
return securityAuthorization.requireAdminUser();
}
],
$get: [
'security',
'securityRetryQueue',
function(security, queue) {
var service = {
requireAdminUser: function() {
},
};
return service;
}
]
});
The actual authorization service appears in the $get property. We place the route
resolve helpers, such as requireAdminUser(), as methods on the provider. When
configuring our routes, we simply inject the provider, and then use these methods
as follows:
config([
'securityAuthorizationProvider',
function (securityAuthorizationProvider) {
$routeProvider.when('/admin/users', {
resolve: securityAuthorizationProvider.requireAdminUser
});
}
])
Now our routes are checked before we navigate to them, and the user is given an
opportunity to authenticate with suitably authorized credentials, if necessary.