Skip to content

Instantly share code, notes, and snippets.

@chriswessels
Created October 8, 2014 11:22
Show Gist options
  • Save chriswessels/76a64c421170095eb871 to your computer and use it in GitHub Desktop.
Save chriswessels/76a64c421170095eb871 to your computer and use it in GitHub Desktop.
/*****************************************************************************/
/* Client and Server Routes */
/*****************************************************************************/
Router.configure({
layoutTemplate: "masterLayout",
notFoundTemplate: "notFound",
});
Router.route("home", { path: "/" });
Router.route("feed", { path: "/feed" });
Router.route("about", { path: "/about" } );
Router.route("snackNew", { path: "/snack/new" } );
Router.route("snackEdit", { path: "/snack/:_id/edit" });
Router.route("snackShow", { path: "/snack/:_id" });
Router.route("snacksIndex", { path: "/snacks" });
Router.route("login", { path: "/login" });
Router.route("signUp", { path: "/sign-up" });
Router.route("giveFeedback", { path: "/feedback" });
/* Loading indicator */
Router.onBeforeAction(manageLoadingIndicator, { except: ['giveFeedback'] });
Router.onAfterAction(hideLoadingIndicator, { except: [] });
Router.onStop(hideLoadingIndicator, { except: [] });
function manageLoadingIndicator (pause) {
if (this.ready()) {
Session.set('loading', false);
this.next();
} else {
Session.set('loading', true);
pause();
}
}
function hideLoadingIndicator () {
Session.set('loading', false);
}
/* Route authorization */
Router.onBeforeAction(mustBeLoggedIn, { except: ["login", "home", "signUp"] });
Router.onBeforeAction(mustBeLoggedOut, { only: ["login", "home", "signUp"] });
function mustBeLoggedIn() {
if (!Meteor.user() && !Meteor.loggingIn()) {
// Ideally we just want to render the login template and stop the route loading. This retains the URL.
// Then upon login, the original route should re-render now that the user is authenticated.
this.render("login");
} else {
this.next();
}
}
function mustBeLoggedOut() {
if (Meteor.user()) {
/* Set the default 'logged in' landing page here */
Router.go("feed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment