Reputation: 7225
I need to perform a $http request before any of my application loads up, so no UI, controllers, services, etc.
The purpose of the request is to check if the user has a valid session on the server based on the presence of a named cookie in the header.
So using a promise I can process the response and if not a valid session, or none exists, then redirect the user to my login page.
So my question, is where should the initial session check request live, my initial thoughts are to use the run block when setting up the application module.
Does this make sense or should I take a different approach?
Upvotes: 0
Views: 37
Reputation: 1079
The problem when doing this in the run config, you cannot put the intialisation of your application on hold. This means, that the first page will get loaded and the dependencies (controllers, services) will get initialized. If you want to do this check before everything gets initialized you will have to do the request before angular bootstraps.
You can bootstrap Angular manually if you remove the ng-app from your html element and load at the end of your index.html a script with:
angular.bootstrap(document, ["myAppModule"]);
You can also make a $http request before you bootstrap angular. In your case this will look something like this:
var initInjector = angular.injector(["ng"]);
var $http = initInjector.get("$http");
return $http.get("config.json").then(function (response) {
//this is to load the config to a constant
angular.module("myAppModule").constant("config", response.data);
angular.bootstrap(document, ["myAppModule"]);
}, function (errorResponse) {
console.error("config not found!");
});
Upvotes: 1