Library to provide user authentication and access management, browser-based JavaScript client Angularjs applications.
To install ngAuthMiddleware use npm
npm install ng-auth-middleware --save
- angular
- angular-permission
- angular-cookies
- @uirouter/angularjs
Inject the ngAuthMiddleware
in your module and $authProvider
to config the module while configuring your angular-application.
var app = angular.module("myApp", ["ngAuthMiddleware"]);
app.config([
"$authProvider",
function($authProvider) {
$authProvider.configure({
withPermission: true, // by default set false
rolePropertyName: "userRole", // property name of role which become from server
permissionPropertyName: "userPermissions", // property name of permission which become from server.
roles: [
{
action: "href", // href or state
roleName: "default", // name of each role
afterSignIn: "applicant", // login according to role
afterLogOut: "authorization.login" // logout according to role
}
]
});
}
]);
There is a full example in the sample Folder.
App.congif.$inject = ["$authProvider"];
function AppConfig(authProvider) {
authProvider.configure({
withPermission: true,
permissionPropertyName: "userPermissions",
rolePropertyName: "userRole",
roles: [
{
action: "href",
roleName: "default",
afterSignIn: "https://www.npmjs.com/package/ng-auth-middleware",
afterLogOut: "https://www.npmjs.com"
},
{
action: "state",
roleName: "admin",
afterSignIn: "admin.panel.dashboard",
afterLogOut: "authorization.login"
},
{
action: "state",
roleName: "multiRole",
afterSignIn: "module.panel.selectedRole",
afterLogOut: "authorization.login"
}
]
});
}
It takes a param where the new role name is assigned, this method remove all roles of user and replace new role which get from param. you can use this method when you have multirole and choose one of them and remove rest of it.
$auth.updateRole(roleName);
return store data.
$auth.user();
It stores the information coming from the server and directs the user to the target given the role and address entered in the configuration.
$auth.signIn(response);
Invokes the method afterSingIn() if the user does not have access to a page
$auth.notAuthorized();
Returns a boolean whether or not the user is authenticated
$auth.isAuthenticated();
First it clears all data and roles Given the role and address entered in Configuration, it redirects the user to that target.
auth.logOut();
npm start and then browse to http://localhost:5000.
Some initial docs are here.
Inject ngAuthMiddleware
in app module.
var app = angular.module("myApp", ["ngAuthMiddleware"]);
Inject $authProvider
in config file, as mentioned above the object exist in authProviderConfigure()
has some properties which want explain here:
authProvider.configure({
withPermission: false,
rolePropertyName: "userRole",
roles: [
{
action: "state",
roleName: "defult",
afterSignIn: "app.dashboard",
afterLogOut: "app.login"
}
]
});
Could set false or true, if set false you haven't connection with angular-permission else you have connected with angular-permission, if you dont set value by default set false.
The response you receive from the server in which property the permissions are assigned. if 'withPermission' is true, it will be requirement.
The response you receive from the server in which property the roles are assigned.
This property is array which set objects,each object says how to behave with each role:
Could set href or state, href uses for change url in one project externally, state uses for change url in one project internally.
Could set name of role and you can set just one role, if your project has multiRole, you have to set multiRole for value of roleName and go to selected role page to choose one role, and at the end you have to set default for one of objects which exist in roles array.
As is clear from the word, which page to go after login.
As is clear from the word, which page to go after logOut.
All are welcome on the issue tracker.