![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)
An authentication and authorization helper service for Angular client applications.
Dependencies
Basic Setup
Add this module to your app as a dependency:
var app = angular.module('yourApp', ['authentication.service']);
Inject $authentication as a parameter in declarations that require it:
app.controller('yourController', function($scope, $authentication){ ... });
Configuration Options
To override the default configuration options, configure the module with an options argument during application configuration and provide overrides for any of the following options.
app.config(['$authenticationProvider', function ($authenticationProvider) {
$authenticationProvider.configure({
authCookieKey: undefined,
storageService: undefined,
profileStorageKey: '$authentication.user.profile',
lastAttemptedUrlStorageKey: '$authentication.last-attempted-url',
onLoginRedirectUrl: '/',
onLogoutRedirectUrl: '/',
notAuthorizedRedirectUrl: '/',
notAuthenticatedRedirectUrl: '/',
trackLastAttemptedUrl: true,
userRolesProperty: 'roles',
extensions: undefined,
expirationProperty: undefined,
events: {
loginConfirmed: 'event:auth-loginConfirmed',
loginRequired: 'event:auth-loginRequired',
logoutConfirmed: 'event:auth-logoutConfirmed',
notAuthenticated: 'event:auth-notAuthenticated',
notAuthorized: 'event:auth-notAuthorized'
},
rolesFunction: function (userProfile) {
if (_.has(userProfile, this.userRolesProperty)) {
var roles = userProfile[this.userRolesProperty];
return _.isArray(roles) ? roles : [roles];
}
return [];
},
validationFunction: function (userRoles, allowedRoles) {
return !_.isEmpty(userRoles) && !_.isEmpty(allowedRoles) &&
(_.find(allowedRoles, function (role) { return _.includes(userRoles, role); }) !== undefined);
},
reauthentication: {
fn: function () {},
timeout: 1200000,
timer: undefined
}
});
}]);
Extensions
All properties (own and inherited) of the extensions object will be available as native to the $authentication service API. The extensions object is applied using the _.defaults(...) method and cannot overwrite any of the existing API properties. This is intended to provide implementors with a way to add objects or functions that are application specific and should fall within the context of the authentication service to expose, e.g., functions to check if a profile has specific roles.
Storage Service Option
If you do not provide a storage service then a simple, in-memory dictionary will be used.
You can provide any storage service or object that supports the following API:
any get(key)
boolean has(key)
void remove(key)
void set(key, value)
To configure a storage service for the authentication provider you provide the service name:
app.config(['$authenticationProvider', function ($authenticationProvider) {
$authenticationProvider.configure({
storageService: '$store'
});
}]);
or an object that provides the expected functionality:
app.config(['$authenticationProvider', function ($authenticationProvider) {
$authenticationProvider.configure({
storageService: new CustomStorageService()
});
}]);
The ng-authentication-service was designed in tandem with the ng-local-storage-service.
API
isAuthenticated()
$authentication.isAuthenticated();
isAuthCookieMissing()
$authentication.isAuthCookieMissing();
isProfileExpired()
$authentication.isProfileExpired();
loginConfirmed(data)
$authentication.loginConfirmed({ ... });
Broadcast via: event:auth-loginConfirmed
with the data
parameter as an argument.
checkAndBroadcastLoginConfirmed()
$authentication.checkAndBroadcastLoginConfirmed();
loginRequired()
$authentication.loginRequired();
Broadcast via: event:auth-loginRequired
.
logoutConfirmed(doNotRedirect)
$authentication.logoutConfirmed();
Broadcast via: event:auth-logoutConfirmed
.
allowed(...)
$authentication.allowed('anonymous');
$authenticated.allowed('all');
$authenticated.allowed('role1', 'role2', ...);
$authentication.allowed('X', ['Y', 'Z'], [['A']]) === $authentication.allowed('X', 'Y', 'Z', 'A')
profile()
$authentication.profile(data);
roles()
$authentication.roles();
isInAllRoles(...)
$authentication.isInAllRoles('role1', 'role2', ...);
$authentication.isInAllRoles('X', ['Y', 'Z'], [['A']]) === $authentication.isInAllRoles('X', 'Y', 'Z', 'A')
isInAnyRoles()
$authentication.isInAnyRoles('role1', 'role2', ...);
$authentication.isInAnyRoles('X', ['Y', 'Z'], [['A']]) === $authentication.isInAnyRoles('X', 'Y', 'Z', 'A')
permit(...)
$authentication.permit('role1', 'role2', ...);
$authentication.permit('X', ['Y', 'Z'], [['A']]) === $authentication.permit('X', 'Y', 'Z', 'A')
getConfiguration()
$authentication.getConfiguration();
getLastAttemptedUrl(fallback)
$authentication.getLastAttemptedUrl();
setLastAttemptedUrl(value)
$authentication.setLastAttemptedUrl();
store()
$authentication.store();
reauthenticate()
$authentication.reauthenticate();
$onLoginConfirmed(handler)
$authentication.$onLoginConfirmed(function (event, data) { ... });
$onLoginRequired(handler)
$authentication.$onLoginRequired(function (event) { ... });
$onLogoutConfirmed(handler)
$authentication.$onLogoutConfirmed(function (event) { ... });
$onNotAuthenticated(handler)
$authentication.$onNotAuthenticated(function (event, data) { ... });
$onNotAuthorized(handler)
$authentication.$onNotAuthorized(function (event, data) { ... });
Development
After forking you should only have to run npm install
from a command line to get your environment setup.
After install you have two gulp commands available to you:
gulp js:lint
gulp js:test