Search code examples
angularjsangular-ui-router

Angular js routing and event flow best practices


I am trying to determine what is the best approach to adopt when a user is already logged in and type /login in the browser URL.

  • Should I prevent the event to be fired by using $routeChangeStart or $locationChangeStart (which one to chose)

  • Should I instead redirect to /home ?

  • Where should I implement the event capture ? In the controller or the app.js


Solution

  • I think you have somewhere a service that tells you if the user is logged in or not. My approach is to use a "resolve" clause/guard in the route/state you want to protect. In this case (login page) you could say something like: are you already logged in? Redirect to /home. Are you not logged in? I leave you load the page and log in. The choice to redirect to /home or elsewhere it depends on your application. You could even redirect to the /my-profile page if you have it.