Base group call to redirect() doesn't cancel call to extended group waitOn()
drone1 opened this issue · 6 comments
I'm having an issue:
I've got a nested/child group, and although the base group redirects, the child group's waitOn() gets called. Is this expected? This is best illustrated with the code.
export const loggedIn = FlowRouter.group({
triggersEnter: [
(context, redirect) => {
if (!Meteor.user()) {
redirect('/login')
return
}
}
]
});
export const adminRoutes = loggedIn.group({
name: "adminRoutes",
prefix: "/admin",
waitOn(params, query, ready) {
throw `Why is this being called?`
},
})
adminRoutes.route('/', {
action(params, query, data) { /* ... */ }
})
As you can see, one can't rely on Meteor.user() because redirect() call in loggedIn.triggersEnter doesn't cancel call to adminRoutes.waitOn().
Thanks!
ostrio:flow-router-extra@v3.7.5
(ostrio:flow-router-meta@2.1.1)
Meteor@2.1
Chrome@91.0.4472.77 (Official Build) (arm64)
Mac Book Pro, Big Sur@11.3.1
Hello @drone1 ,
There's no way to alter this behavior, method called triggersEnter
, not triggersBeforeEnter
, meaning that triggers actually called after waitOn
, since by architecture you may need to pre-fetch data in waitOn
to use it later in triggersEnter
hooks
Hope that helps.
Feel free to reopen it in case if the issue is still persists on your end.
P.S. I'm sorry for the late reply
Hello @drone1 ,
There's no way to alter this behavior, method called
triggersEnter
, nottriggersBeforeEnter
, meaning that triggers actually called afterwaitOn
, since by architecture you may need to pre-fetch data inwaitOn
to use it later intriggersEnter
hooksHope that helps.
Feel free to reopen it in case if the issue is still persists on your end.
P.S. I'm sorry for the late reply
Hi, thanks.
I don't see triggersBeforeEnter so how is one supposed to implement this kind of flow in a clean way? Suggestions would be appreciated.
The docs show an example that does not demonstrate how a super user is actually verified or differentiated from an admin in the "nested groups" example. A redirect or error is necessary. How does one implement this? Thank you!
I don't see triggersBeforeEnter so how is one supposed to implement this kind of flow in a clean way? Suggestions would be appreciated.
There's no such function, that's what I meant.
Try next approach:
const isAdmin = () => {
// verify admin user permissions
// You can return promise and waitOn for Method response
};
const adminGroup = FlowRouter.group({
prefix: '/admins',
name: 'admin',
title: 'Admin Panel',
meta: {
robots: 'noindex, nofollow'
}
});
adminGroup.route('/', {
name: 'adminHome',
waitOn() {
if (isAdmin()) {
return import('/imports/client/Admin/index/index.js');
}
},
action() {
if (isAdmin()) {
this.render('layout', 'adminHome');
} else {
this.render('notfound404');
}
}
});
Let me know if this helps
@drone1 to be honest in most of the apps we use Blaze, and implement this logic on the level of the template or "adminLayout" where if user is verified as Admin — pull data and render the template, if not show login or 404 page. Moving data logic to the template level as well.
Hope that helps