This is a role based authentication module for angular.js. This modules is meant to provide two bits of functionality:
This module is not meant to provide the authentication mechanism or the resource for learning about the roles. Frankly, I don't want to provide that because it would just make the module more complicated and force you to use something that you probably don't want to use anyway.
This utilizes the angular router (ngRoute
), so simply add this module and that module as dependencies.
var app = angular.module("MyAwesomeApp", ["ngRoute", "ngRoleAuth"]);
Then overload the AuthService
getRole
method. This method should return the role or roles of the current
user of the system. You can overload it anywhere, but the run
method seems like a good place. Note that you
can return a single string or an array of strings for the current role. Also note that if you want to do some async stuff
before you can cache the role, it will update the UI with the new role.
app.run(["AuthService", function(authService){
auth.getRole = function(){
return "admin"; // this could also return an array
};
}]);
Routes are very easy to assign roles to. Just set the authorized
parameter.
app.config(["$routeProvider", function($routeProvider){
$routeProvider
.when("/admin-route", {
templateUrl: "partials/admin-route.html",
authorized: "admin"
})
.when("/user-admin-route", {
templateUrl: "partials/user-admin-route.html",
authorized: ["user", "admin"]
})
.when("/admin-route", {
templateUrl: "partials/admin-route.html",
authorized: ["admin"]
})
.otherwise("/");
}]);
Elements are also easy to assign roles to. You simply need to use the nra-auth
directive.
<a href="#/admin-route" nra-auth="'admin'">Only shows for Admin</a>
<a href="#/user-admin-route" nra-auth="['admin', 'user']">Shows for both Admin and User</a>
Development should be fairly straightforward. The first time you start do the following:
$ npm install
$ bower install
$ grunt
The repo is structure in a fairly standard way.
./dist
is where the built files live. They are checked in so that we can make useful releases easily. ./src
is where the source files live. ./example
is where the example application lives. Check it out on the development server by
browsing to localhost:9000/example. BSD 2-Clause -- Do whachya wanna!