Background
My app needs to make a request to the server to verify whether or not the user has an authenticated session, before the first state is loaded. As depending on on the url, the user may need to be redirected to the login page.
i.e if the initial url is myapp.com/#/profile
and the user doesn't have a pre-existing authenticated session, they'll be redirected to myapp.com/#/login
Problem
I have logic in place to deal with this situation, but immediately after the session data request is made to the server and before it completes, the initial state change is triggered and the page loads and displays, before the server responds
Possible Solutions
1) I'm using django as my backend, so could just remove the initial server request from the app and pass the data direct to the app in the django template. Either through
ng-init of the main controller - although i've read that this is a bad idea, as it's not it's intended usage
Or in a script tag in the django template - which i'm loathe to do, as i'd rather keep all my application logic seperate in js files
2) Listen for the initial $stateChangeStart, cancel it and then call $state.reload() once the server requests completed
Question
It would seem to me to be a fairly common requirement to fulfill certain criteria before the initial state change occurs, so i'm wondering if i've missed something in the docs and there's already an inbuilt way to deal with this scenario?
Or is there a way to deactivate the $urlRouterProvider/$stateProvider to prevent the initial state change from occurring?
resolve
property for routes: docs.angularjs.org/api/ngRoute/provider/$routeProvider - I use this for exactly what you're talking about. In theresolve
callback, I make an AJAX request to my server to get user info from the session. If nothing is returned, that means they're not authenticated and I reject the promise that is used in theresolve
resolve
is all I could find in my extensive searching when I needed a solution for something like this. It seems like any form of listening for a state change (or whatever) doesn't allow you to "resume" or "cancel" (only "cancel").