Fixes some issues with app startup/authentication and when events fire

This commit is contained in:
Shannon
2013-12-11 12:01:22 +11:00
parent 85bf47df82
commit 0de53308f5
4 changed files with 40 additions and 24 deletions

View File

@@ -42,7 +42,8 @@ function appState(eventsService) {
touchDevice: null,
showTray: null,
stickyNavigation: null,
navMode: null
navMode: null,
isReady: null
};
var sectionState = {

View File

@@ -11,31 +11,19 @@ app.run(['userService', '$log', '$rootScope', '$location', 'navigationService',
xhr.setRequestHeader("X-XSRF-TOKEN", $cookies["XSRF-TOKEN"]);
}
});
var firstRun = true;
/** Listens for authentication and checks if our required assets are loaded, if/once they are we'll broadcast a ready event */
eventsService.on("app.authenticated", function(evt, data) {
assetsService._loadInitAssets().then(function() {
appState.setGlobalState("isReady", true);
//send the ready event
eventsService.emit("app.ready", data);
});
});
/** when we have a successful first route that is not the login page - *meaning the user is authenticated*
we'll get the current user from the user service and ensure it broadcasts it's events. If the route
is successful from after a login then this will not actually do anything since the authenticated event would
have alraedy fired, but if the user is loading the angularjs app for the first time and they are already authenticated
then this is when the authenticated event will be fired.
*/
/** execute code on each successful route */
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
if (firstRun && !$location.url().toLowerCase().startsWith("/login")) {
userService.getCurrentUser({ broadcastEvent: true }).then(function (user) {
firstRun = false;
});
}
if(current.params.section){
$rootScope.locationTitle = current.params.section + " - " + $location.$$host;
}

View File

@@ -1,5 +1,5 @@
app.config(function ($routeProvider) {
/** This checks if the user is authenticated for a route and what the isRequired is set to.
Depending on whether isRequired = true, it first check if the user is authenticated and will resolve successfully
otherwise the route will fail and the $routeChangeError event will execute, in that handler we will redirect to the rejected
@@ -8,7 +8,7 @@ app.config(function ($routeProvider) {
return {
/** Checks that the user is authenticated, then ensures that are requires assets are loaded */
isAuthenticatedAndReady: function ($q, userService, $route, assetsService) {
isAuthenticatedAndReady: function ($q, userService, $route, assetsService, appState) {
var deferred = $q.defer();
//don't need to check if we've redirected to login and we've already checked auth
@@ -21,14 +21,33 @@ app.config(function ($routeProvider) {
.then(function () {
assetsService._loadInitAssets().then(function() {
//is auth, check if we allow or reject
if (isRequired) {
//this will resolve successfully so the route will continue
deferred.resolve(true);
//This could be the first time has loaded after the user has logged in, in this case
// we need to broadcast the authenticated event - this will be handled by the startup (init)
// handler to set/broadcast the ready state
if (appState.getGlobalState("isReady") !== true) {
userService.getCurrentUser({ broadcastEvent: true }).then(function(user) {
//is auth, check if we allow or reject
if (isRequired) {
//this will resolve successfully so the route will continue
deferred.resolve(true);
}
else {
deferred.reject({ path: "/" });
}
});
}
else {
deferred.reject({ path: "/" });
//is auth, check if we allow or reject
if (isRequired) {
//this will resolve successfully so the route will continue
deferred.resolve(true);
}
else {
deferred.reject({ path: "/" });
}
}
});
}, function () {