User authentication with $rootScope

Asked

Viewed 367 times

2

I am developing a system where depending on the level of the user 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 higher than 0, he is a broker and will be redirected to his page, but he will be able to access another page

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

and that way it’s my $rootscope and will always be directed to /ContratosCorretor.. Does anyone have a solution to this problem?

  • If I understand well what you want to do, depending on the level of access the user is redirected to a page is this?

  • That, depending on his level, he’s redirected to a page, but he can only access that page... I want him to be able to access one more

  • The user only gets stuck on that page, but I want him to be able to browse through one more

  • In this case, I understand that when the user accesses the first time will be redirected to a default page. Then he can navigate between pages through a I believe menu. Is that it? If yes what you need is a dynamic menu that is compiled at runtime according to the user’s level. Right?

2 answers

2


I suggest you determine a level variable before setting the routes this way:

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 do the level check in 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 the 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") ;
          }
       ...

1

I believe that analyzing a rule on every route that is only valid after login is a waste of processing. You can register a specific System 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");
  }
});

This way, you will only redirect to the home page when the object is effectively changed, either by login or something like that. On all other navigation routes, it will work properly.

Just as an add-on, your code didn’t work because you gave a Binding in the route swap event, so whenever the route was different from the login route, you automatically redirected the user to your homepage.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.