Uncaught RangeError: Maximum call stack size exceeded at new e.Computation

Hello

i did a tracker in triggersEnter of flowrouter i have this code

my code

function trackDashboardsEntry(context, redirect) {
Tracker.autorun(function (handle) {
console.log(“handle”,handle);
if (!Meteor.userId()) {

        FlowRouter.go("/login");
       
    } else {
        if (Meteor.user() && Meteor.user().profile) {
             if (Meteor.user().roles.app[0] == "user") {
             } else {
                FlowRouter.go("/login");
             }
         
            return 
        } else {
            FlowRouter.go("/login");
        }
    }
    handle.stop();
});

}

please can someone give me the best practice to verif . user role before page access

Please any one can help me?

Hi @neutron92,
A Maximum call stack size exceeded error points to some kind of infinite loop.

I’m if this function is triggered on every route, then it runs on first load and redirects to /login, which triggers this function, which redirects to /login which is an infinite loop.

There’s not really any reason to use a Tracker here, because by the time it re-runs you will have already been redirected and the only other result is to do nothing.

You will also want to add an extra check that you’re not already on the login route before redirecting:

if (FlowRouter.current().route.name !== "login") {
  FlowRouter.go("/login");
}

Mind you, your function could be simplified greatly on top of these two changes:

function trackDashboardsEntry(context, redirect) {
    if (context.route.name !== "login") return; // exit immediately if login page

    const user = Meteor.user(); // Only lookup the user once, since it can be expensive.
    if (user && user.roles && user.roles.app && user.roles.app[0] === "user") {
        return; // If there's a user, we're done
    }
    redirect("/login"); // prefer redirect for redirection
}

If you need to wait on data to be published before performing any route checks, you can tell FlowRouter to wait:

// Example for alanning:roles
FlowRouter.wait();
Tracker.autorun(() => {
    // if the roles subscription is ready, start routing
    // there are specific cases that this reruns, so we also check
    // that FlowRouter hasn't initalized already
    if (Roles.subscription.ready() && !FlowRouter._initialized) {
        FlowRouter.initialize();
    }
});
// Route definitions follow
2 Likes