I would like to register routes in my angular application based on the roles of the user, can I do something like:
angular.module('myModule', [])
.config(function($routeProvider, $http){
$routeProvider.when('/home',{
templateUrl: '/home.html',
controller: 'HomeCtrl'
});
$http.get('/user')
.success(function(user){
if (user.admin){
$routeProvider.when('/dashboard, {
templateUrl: '/dashboard.html',
controller: 'DashboardCtrl'
});
}
});
});
But in the config
method I can't use the $http
service, how can I achieve it?
What is Role-Based Authorization? In the system's security, the role-based authorization / role-based access control (RBAC) is a way of restricting and managing machine access to authorized users. RBAC (Role-based access control) confines access to the network based on the employee's role in an organization.
Angular route guards are interfaces provided by angular which when implemented allow us to control the accessibility of a route based on condition provided in class implementation of that interface. Five types of route guards are provided by angular : CanActivate.
Include AuthGuard to Route Now for each request of the route, the verify() function will be called and if the verify() function returns true, then only we can access the particular route. If it returns false, we are not able to access it. This is how we can restrict the routes from unauthorized user access.
AuthGuard is used to protect the routes from unauthorized access in angular. How AuthGuard Works? Auth guard provide lifecycle event called canActivate. The canActivate is like a constructor. It will be called before accessing the routes.
Check out ui-router. It is a much more feature-rich state-based routing system for Angular. It solves your problem by allowing you to make a model request at the moment of your state change, and return a promise so that when you get your model, you can either continue or change state again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With