I have a very simple requirement.
I have 3 User Roles:
- CATUSER
- LICUSER
ALLUSER
- I have the value of the User Role in the
$rootScope.userRolevariable. - I have the User Role already defined before the AngularJS application starts because AngularJS app is called from a PHP script and User Role is already defined in the PHP script.
- I have the value of the User Role in the
Now, when the AngularJS app starts, according to the Role I want to have following Routes:
$rootScope.userRole == "CATUSER"
if ($rootScope.userRole == "CATUSER") {
$routeProvider
.when("/catheter", {
title: "Catheter Expiration Code Generator",
templateUrl: "app/catheter/catheter.html",
controller: "CatheterController",
controllerAs: "vm"
})
.when("/support", {
title: "Support",
templateUrl: "app/support/support.html",
controller: "SupportController",
controllerAs: "vm"
})
.otherwise({
redirectTo: "/catheter"
});
}
$rootScope.userRole == "LICUSER"
if ($rootScope.userRole == "LICUSER") {
$routeProvider
.when("/license", {
title: "License Generator",
templateUrl: "app/license/license.html",
controller: "LicenseController",
controllerAs: "vm"
})
.when("/support", {
title: "Support",
templateUrl: "app/support/support.html",
controller: "SupportController",
controllerAs: "vm"
})
.otherwise({
redirectTo: "/license"
});
}
$rootScope.userRole == "ALLUSER"
if ($rootScope.userRole == "LICUSER") {
$routeProvider
.when("/license", {
title: "License Generator",
templateUrl: "app/license/license.html",
controller: "LicenseController",
controllerAs: "vm"
})
.when("/catheter", {
title: "Catheter Expiration Code Generator",
templateUrl: "app/catheter/catheter.html",
controller: "CatheterController",
controllerAs: "vm"
})
.when("/support", {
title: "Support",
templateUrl: "app/support/support.html",
controller: "SupportController",
controllerAs: "vm"
})
.otherwise({
redirectTo: "/license"
});
}
I don't want to use UI Router.
I use UI Router for this kind of purpose in past. Here is the sample code to get you started
Here you see License route has property data. Its required authentication and it is authorized for LICUSER and CATUSER roles. You can also add more permission check here for example read, write etc. If user is authenticated and authorized the requested the requested state will load up else will redirect to login or denied request.