简体   繁体   中英

Validating routes in angularjs

Right now I'm working on a one page angularjs app using angular to do the routing. To log in a user I validate their username and password using an $http request, I then redirect them to /user_home. The problem is that the user who is not logged in can go to /user_home without logging in. I could easily validate if they were logged in by checking for the csrf cookie and localstorage that I create on login but where should this validation be placed?

use

$rootScope.$on('$routeChangeStart', function(evt, absNewUrl, absOldUrl) {
//your validation function based on url and set $location accordingly

  });

to check before route is rendered

or

$rootScope.$on('$routeChangeSuccess', function(evt, absNewUrl, absOldUrl) {
//your validation function based on url and set $location accordingly

 });

to check after page is rendered

more info in ANGULAR DOCS ROUTE

Here is FIDDLE of how to implement it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM