Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular $routeProvider based on user role

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?

like image 509
rascio Avatar asked Oct 28 '13 10:10

rascio


People also ask

What is role based access control in angular?

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.

What are the responsibilities of a route Guard in angular?

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.

How would I restrict access to routes in angular?

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.

What is AuthGuard angular10?

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.


1 Answers

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.

like image 139
Brian Genisio Avatar answered Sep 19 '22 21:09

Brian Genisio