How to display a menu according to the type of user logged in with Ionic / Angularjs?

Asked

Viewed 595 times

1

Hello, I have two types of users: buyer and seller.

How to display a menu according to the type of user logged in with Ionic / Angularjs?

Currently, and does not work well, I am putting on each menu item an ng-show:

ng-show="permissaoUser"

And in the Menu Controller:

if($window.localStorage.getItem("user_cod_cliente") === null){
            $scope.permissaoUser = false;
        }else{
            $scope.permissaoUser = true;
        }      

        if($window.localStorage.getItem("global_fornecedor") === null){
           $scope.permissaoVovo = false;
        }else{
            $scope.permissaoVovo = true;
        }      

That is, I get the information saved in my Localstorage after login.

It happens that the menu only presents according to the user SE der F5 in the BROWSER that is where I follow the development.

However, the emulator or the device does not work.

Anyone can help?

Thank you.

  • Why not send these menus, in vector form, to your application. Then, create a directive that assembles the menu according to this vector. I believe that to treat this in the backend would be more sensible and easier.

1 answer

2


Switch to a function as follows:

ng-show="checarPermissoes('cliente')"

or ng-show="checkPermissions('client')"

And the function:

$scope.verificarPermissoes(tipo) {
  var permitido;

  if (tipo === 'cliente' && $window.localStorage.getItem("user_cod_cliente") !== null) {
    permitido = true;
  } else if (tipo === 'fornecedor' && $window.localStorage.getItem("global_fornecedor") !== null) {
    permitido = true;
  } else {
    permitido = false;
  }

  return permitido;
}
  • Note that this alone does not prevent the malicious user from seeing this part of the site; also try to check, on the server side, when users do actions from this page to ensure that the one who accesses the API is who Voce thinks it is

  • 1

    @Moshmage actually the ideal, in addition to what you suggested, is to do this check in the states and only make them available if the user has permission

  • Solved with the @Sorack tip

  • Thank you @Sorack :)

  • @Branches don’t forget to mark the answer as resolution by clicking on the left side of the answer so it can be used by other users

Browser other questions tagged

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