0

I am using Meteor with FlowRouter and I am getting a strange behavior. I am wondering if I am just fundamentally not understanding something. I have the following code in client/route.js:

"use strict";

FlowRouter.route('/', {
    name: 'home',
    action: function() {
        BlazeLayout.render('main', {main: "homePage"});
    }
});

FlowRouter.route('/admin', {
    name: 'admin',
    triggersEnter: [isUserLoggedIn],
    action: function() {
        BlazeLayout.render('main', {main: "admin"});
    }
});

function isUserLoggedIn() {
    console.log("Worked");
    if (Meteor.userId()) {
        route = FlowRouter.current();
    } else {
        FlowRouter.go("home");
    }
}

I run meteor and I go to localhost:3000 and I look at the console, I see "Worked" meaning that the isUserLoggedIn function has been triggered. I have NOT clicked on admin or gone to localhost:3000/admin. Only to the top level. Why would the isUserLoggedIn function be triggered when I have not gone to the /admin route?

Edit 1: It would seem my simplified example actually does work pretty well. The actual problem is actually a bit more like this:

"use strict";

FlowRouter.route('/', {
    name: 'home',
    action: function() {
        BlazeLayout.render('main', {main: "homePage"});
    }
});

FlowRouter.route('/admin', {
    name: 'admin',
    triggersEnter: [isUserLoggedIn(role)],
    action: function() {
        BlazeLayout.render('main', {main: "admin"});
    }
});

function isUserLoggedIn(role) {
    console.log("Worked");
    if (Meteor.userId() && Role.userIsInRole(role)) {
        route = FlowRouter.current();
    } else {
        FlowRouter.go("home");
    }
}

Passing a parameter through the triggersEnter doesn't seem to be possible (or I am not aware of how to make it work properly). Is there a way to send a parameter through the triggersEnter?

Tim C
  • 1,934
  • 12
  • 25

1 Answers1

2

You need to return a function from your isUserLoggedIn function, like this:

function isUserLoggedIn (role) {
  return function (context, redirect, stop) {
    if (Meteor.userId() && Roles.userIsInRole(Meteor.userId(), role)) {
      route = FlowRouter.current();
    } else {
      FlowRouter.go("home");
    }
  }
}
Tiberiu Maxim
  • 1,474
  • 14
  • 24
  • To the top ! The only way to work for me but with update on line 3: `if (Meteor.userId() && Roles.userIsInRole(Meteor.userId(), role)) {` – Arthur Jan 12 '16 at 16:13