1
I have a system where I log in a user, in case everything right the server returns me an object and I store this object in sessionStorage
. I can recover this object to do some validations, e.g.: render some components according to user permission.
I would like to block some routes according to the user’s permission, that is, if the user has permissão = 2
he won’t be able to access that route: /usuario
The route configuration is as follows:
angular.module("oraculo").config(function($routeProvider){
$routeProvider.when("/usuario", {
templateUrl: "public/views/usuario.html",
controller: "usuarioCtrl"
});
$routeProvider.otherwise({redirectTo: "/login"});
});
I got it, that’s what I’d do in this
config
of routes or in thecontroller
of each page?– DiegoAugusto
Like
broadcast
s can be announced for the entire application, this code could be within a service - responsible only for monitoring route changes and redirecting if necessary.– OnoSendai
@Techies I included an example to be entered in the route configuration as well.
– OnoSendai
Beauty kk, I’m thinking, would be able to use the
resolve
to make that validation?– DiegoAugusto
@Techies yes, it is possible. However I have included another suggestion that may be more useful to you, check the edited reply.
– OnoSendai
All right, I’ll try it this way.
– DiegoAugusto
@Onosendai The problem of validating in the service (server?) is that the otherwise expects function or a string with the route or a function that will be executed synchronously, requests as they are asynchronous would not be expected, so the next block would continue the execution. One solution I found are the future states, which implement asynchronous routes, allowing greater flexibility, see in this link
– EProgrammerNotFound
@Onosendai In this case, I leave the comment to the AP if it wants something more flexible, particularly I would prefer Future States if my application only consumed an API, because at each exchange of states I could check if the authentication token is still valid. Although, I do not know if it would reduce the scalability of the application when doing this, but I believe that I do not know... rs
– EProgrammerNotFound
@Eprogrammernotfound The solution you presented is valid - what I meant by the last part is that you can, for example, load the user’s permissions set from the JS side, and via synchronous validation control the flow. Via server, a common method is to implement an Injector that, when receiving specific HTTP states (for example
403 Forbidden
), force the route to a page that Handling correct.– OnoSendai
@Onosendai Roger that! Actually, I work with applications that control via server as you mentioned, we are working on a solution to control in the Browser, we are in a dilemma whether we will do as you said, synchronously or we will use Future States. I posted the comment so that if someone had already used this ui-router extra and knew of some problem/ or advantage could also comment, and of course suggest to AP and show it another way.
– EProgrammerNotFound