2 * Core Service - egStartup
4 * Coordinates all startup routines and consolidates them into
5 * a single startup promise. Startup can be launched from multiple
6 * controllers, etc., but only one startup routine will be run.
8 * If no valid authtoken is found, startup will exit early and
9 * change the page href to the login page. Otherwise, the global
10 * promise returned by startup.go() will be resolved after all
11 * async data is arrived.
14 angular.module('egCoreMod')
17 ['$q','$rootScope','$location','$window','egIDL','egAuth','egEnv',
18 function($q, $rootScope, $location, $window, egIDL, egAuth, egEnv) {
20 var service = { promise : null }
22 // returns true if we are staying on the current page
23 // false if we are redirecting to login
24 service.expiredAuthHandler = function() {
25 console.debug('egStartup.expiredAuthHandler()');
26 egAuth.logout(); // clean up
28 // no need to redirect if we're on the /login page
29 if ($location.path() == '/login') return true;
31 // change locations to the login page, using the current page
32 // as the 'route_to' destination on /login
33 $window.location.href = $location
36 $window.location.pathname + $window.location.search})
42 // if during startup or any time in the future we encounter an expired
43 // authtoken, call our epired token handler
44 // we handle this here instead egAuth, since it affects the flow
45 // of the startup routines when no valid token exists during startup.
46 $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
48 service.go = function () {
49 if (service.promise) {
50 // startup already started, return our existing promise
51 return service.promise;
54 // create a new promise and fire off startup
55 var deferred = $q.defer();
56 service.promise = deferred.promise;
58 // IDL parsing is sync. No promises required
60 egAuth.testAuthToken().then(
62 // testAuthToken resolved
65 function() { deferred.resolve() },
67 deferred.reject('egEnv did not resolve')
72 // testAuthToken rejected
74 console.log('egAuth found no valid authtoken');
75 if (service.expiredAuthHandler()) deferred.resolve();
79 return service.promise;