User Authentication with $ rootScope

2

I am developing a system where depending on the user level it is redirected to a page

.run(function($rootScope, $location, $http, config) {
  $rootScope.$on('$locationChangeStart', function(event) {
    if ($location.path() != "/Login") {

      $http.get(config.BaseUrl + "/auth.php").success(function(inf) {
        if (typeof inf == "object") {
          if (inf.nivel > 0) {
            $location.path("/ContratosCorretor") ;
          }

That is, if the user level is greater than 0, he is a broker and will be redirected to his page, but he will be able to access one more page

  $routeProvider.when("/NovoContratoCorretor", {
    templateUrl: "view/NovoContratoCorretor.html",
    controller: "NovoContratoCorretor",
  })

So that's my $ rootscope and it will always be redirected to /ContratosCorretor . Does anyone have any solutions to this problem?

    
asked by anonymous 30.09.2015 / 00:03

2 answers

2

I suggest that you determine a level variable before setting up routes like this:

if (nivel == 1) {
    angularApp.config(
                ['$routeProvider',
                    function ($routeProvider) {
                    $routeProvider
                 .when("/NovoContratoCorretor", {
                        templateUrl: "view/NovoContratoCorretor.html",
                        controller: "NovoContratoCorretor",
                 })
                 .when("/OutraPagina", {
                        templateUrl: "view/OutraTemplate.html",
                        controller: "OutraController",
                 })

               ]);
               }
}

Or if you prefer, you can set a level for each route, and check the level on the controller:

     $routeProvider
                .when("/NovoContratoCorretor", {
                     templateUrl: "view/NovoContratoCorretor.html",
                     controller: "NovoContratoCorretor",
                     niveis: [1,2]
                     })
                .when("/OutraPagina", {
                     templateUrl: "view/OutraTemplate.html",
                     controller: "OutraController",
                     niveis: [2,3]
                });

In control you capture level:

.run(function($rootScope, $location, $http, config, $routeParams) {
  $rootScope.$on('$locationChangeStart', function(event) {
    if ($location.path() != "/Login") {

      $http.get(config.BaseUrl + "/auth.php").success(function(inf) {
        if (typeof inf == "object") {
          if (angular.isDefined($routeParams.niveis[inf.nivel])) {
               $location.path("/ContratosCorretor") ;
          }
       ...
    
07.10.2015 / 21:27
1

I believe that looking at a route that is valid only after login is a waste of processing. You can register a specific listener for the user variable and only redirect it when it changes, as follows:

$rootScope.$watch('nomeDaVariavelDeUsuario', function(novoUsuario) {
  if(novoUsuario.nivel > 0) {
    $location.path("/ContratosCorretor");
  }
});

In this way, you will only redirect to homepage when the object is actually changed, either by login or something. On all other navigation routes, it will work correctly.

Just as an add-on, your code did not work because you gave a binding on the route change event, so whenever the route was different from the login, you automatically redirected the user to your home page.

    
07.10.2015 / 20:47