Context
In order to ensure that the user has an authenticated session, my app must send a request to the server before loading the first state. Depending on the URL, if the user is not authenticated, they should be redirected to the login page.
For example, if the initial URL is myapp.com/#/profile
and the user is not authenticated, they will be redirected to myapp.com/#/login
.
Issue
Although I have implemented logic to handle this scenario, there is a problem where the initial state change triggers before the server responds, causing the page to load prematurely.
Potential Solutions
1) Since I am using Django as my backend, one option is to eliminate the initial server request from the app and directly pass the data to the app in the Django template. This can be achieved through:
Using ng-init in the main controller, despite it not being its intended usage
Embedding the data in a script tag within the Django template, although it may mix application logic with HTML
2) Another solution could be to listen for the initial $stateChangeStart event, cancel it, and then reload the state once the server response is received.
Query
Given that it seems common to require certain criteria to be met before initiating the initial state change, I am curious if there exists an existing method or feature in the documentation to address this situation?
Alternatively, is there a way to deactivate the $urlRouterProvider/$stateProvider to prevent the initial state change from happening?