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')
16 .config(['$routeProvider','$locationProvider','$compileProvider',
17 function($routeProvider , $locationProvider , $compileProvider) {
18 $locationProvider.html5Mode(true);
19 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/);
23 ['$q','$rootScope','$location','$window','egIDL','egAuth','egEnv',
24 function($q, $rootScope, $location, $window, egIDL, egAuth, egEnv) {
26 var service = { promise : null }
28 // returns true if we are staying on the current page
29 // false if we are redirecting to login
30 service.expiredAuthHandler = function() {
31 console.debug('egStartup.expiredAuthHandler()');
32 egAuth.logout(); // clean up
34 // no need to redirect if we're on the /login page
35 if ($location.path() == '/login') return true;
37 // change locations to the login page, using the current page
38 // as the 'route_to' destination on /login
39 $window.location.href = $location
42 $window.location.pathname + $window.location.search})
48 // if during startup or any time in the future we encounter an expired
49 // authtoken, call our epired token handler
50 // we handle this here instead egAuth, since it affects the flow
51 // of the startup routines when no valid token exists during startup.
52 $rootScope.$on('egAuthExpired', function() {service.expiredAuthHandler()});
54 service.go = function () {
55 if (service.promise) {
56 // startup already started, return our existing promise
57 return service.promise;
60 // create a new promise and fire off startup
61 var deferred = $q.defer();
62 service.promise = deferred.promise;
64 // IDL parsing is sync. No promises required
66 egAuth.testAuthToken().then(
68 // testAuthToken resolved
71 function() { deferred.resolve() },
73 deferred.reject('egEnv did not resolve')
78 // testAuthToken rejected
80 console.log('egAuth found no valid authtoken');
81 if (service.expiredAuthHandler()) deferred.resolve();
85 return service.promise;