Search code examples
meteoriron-router

iron-router (Meteor 1.0) Template.layout.rendered not working for private pages


I use the same layout for all my pages. However, when user is logged in, my layout.js no longer works, specifically the Template.layout.rendered function.

How do I deal with this?

Router.configure({
    templateNameConverter: "upperCamelCase",
    routeControllerNameConverter: "upperCamelCase",
    layoutTemplate: "layout",
    notFoundTemplate: "notFound",
    loadingTemplate: "loading"
});

var publicRoutes = ["home_public", "home-public", "login", "register", "forgot_password", "reset_password"];
var privateRoutes = ["home-private", "orders", "orders.insert", "orders.details", "orders.edit"];

Router.ensureLogged = function() {
    if(!Meteor.user()) {
        // user is not logged in - redirect to public home
        this.redirect("home_public");
        return;
    } else {
        // user is logged in - check role
        this.next();
    }
};

Router.ensureNotLogged = function() {
    if(Meteor.user())
        this.redirect("home_private");
    else
        this.next();
};

Router.onBeforeAction(Router.ensureNotLogged, {only: publicRoutes});
Router.onBeforeAction(Router.ensureLogged, {only: privateRoutes});

Solution

  • Seems that Meteor or Iron-Router (I suspect Meteor) has some issues.

    **My fix was to use separate layouts for private and public pages such as layout_private and layout_public. **

    Meteor, when switching between logged in/out, does not re-execute the Template.layout.render (if you use the same layout) and it looses the javascript event listeners somehow in between.