ngSecurity
AngularJS.
A security module forInstall
Installing via Bower:
$ bower install ngsecurity
Installing via NPM:
$ npm install ngsecurity
Get Started
Your setup should look similar to the following:
/* file: app.js */angular ;
<!-- file: index.html --> Login User is authenticated User is Anonymous User is Administrator
/* POST /api/auth response */ "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" /* required */ "user": /* optional */ "name": 'Administrator' "permissions": /* optional */ 'admin'
API
Service Methods
login(token: String, [user: Object, [permissions: Array]]) [Simple strategy]
Authenticate a user with token. Data and permissions are optional.
Simple example:
angular ;
login(token: String, [permissions: Array]) [JWT strategy]
Authenticate a user with token. Permissions are optional.
JWT example:
/* file: app.js */angular ;
loginByUrl(url: String, data: Object)
Use resource for authenticate user. The service should return a response compatible. The return is a promise.
Simple example:
angular ;
Compatible API response:
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" /* required */ "user": /* optional */ "name": 'Administrator' "permissions": /* optional */ 'admin'
JWT example:
/* file: app.js */angular ;
Compatible API response:
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQWRtaW5pc3RyYXRvciJ9.cwtcdrm6c5fXBdnhzkFnlXmvvsRY7xB6YsKrLE_phm4" /* required */
logout()
User logout and remove session user data.
hasPermission(permission: String)
Check if user has permission.
hasAllPermission(permissions: Array)
Check if user has all permission of array.
hasAnyPermission(permissions: Array)
Check if user has any permission of array.
isAuthenticated()
Check if user is authenticated.
getUser()
Get session user data.
getPermissions()
Get session user permissions.
Directives
ng-if-authenticated (Attribute only)
The directive only shows the HTML element if user is authenticated.
ng-if-anonymous (Attribute only)
The directive only shows the HTML element if user not is authenticated.
ng-if-permission="<permission: String>" (Attribute only)
The directive only shows the HTML element if user has permission.
Admin or Staff Admin
ng-if-permission-model="<permission: Any>" (Attribute only)
The directive only shows the HTML element if user has permission specified on scope.
angular ;
Admin or Staff Admin
ng-permission-type="ALL|ANY" (Attribute only)
The auxiliary directive for ng-if-permission, ng-if-permission-model and ng-enabled-permission, it determine the validation method.
The value default is ANY.
Admin or Staff Admin and Staff
ng-enabled-permission="<permission: String>" (Attribute only)
The directive sets the disabled attribute on the HTML element if user has permission.
Add User Remove User
ng-click-logout (Attribute only)
The directive logout current user.
Logout
ng-bind-user="<attribute: String>" (Attribute only)
The directive replace the text content of the specified HTML element with the user data.
<!-- render user name -->
ng-submit-login="<url: String>" (Attribute only)
The directive login user when submit form. It calls back ng-login-success when success to login, and calls back ng-login-error when fail to login.
<!-- show the HTML element when login is successful --> Success! <!-- show the HTML element when login failed --> Username or password invalid! Login
Provider Options
angular ;