A
A
Alexander Tartmin2015-06-18 15:31:45
Angular
Alexander Tartmin, 2015-06-18 15:31:45

Check access to the page. How to implement correctly?

There is a ready-made SPA in which 3 types of users are authorized. I'm trying to write a service that should check the user's account_type whether he has access to the requested page. I got stuck on the test itself.
I run this function through .run

function checkAccess(AccountUtilities, $route, $rootScope) {
        $rootScope.$on('$routeChangeStart', function() {
            AccountUtilities.accessCheck();
        });
    }

AccountUtilities.accessCheck(); - this is the service itself in which the access check should take place, passing the type of account there and checking somehow exactly where the user climbs. If the user does not have access to the requested page, or redirects to his home page, or if the user is not logged in at all, then redirects to the login page.
In the application itself there are pages that are available only to one type of account, general pages that are available to any user (My Account, etc) and "external pages" such as registration or the login page, which, being authorized, the user should not get.
My problem is with verification. I can’t think of a better way to check if I have an account type and a lot of information about routes (I know the current route, a list of all available routes and where the user actually goes). The only solutions I see so far are:
  • in page controllers, set via $rootScope what type of account this page is intended for (I consider this one as the most elegant so far)
  • all routes have common parts of yurls, I thought to tear them out and compare them through a regular program (some kind of crutch method)
  • there are other options, but they are already very perverted as for me

Please share your wisdom and experience on how to properly implement such a check so that when adding routes, you don’t have to constantly add or correct something somewhere.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valentin Dubrovsky, 2015-06-18
@matroskin13

use ui-router. in ui-router, there is a cool feature called resolve.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question