veliovgroup / flow-router

🚦 Carefully extended flow-router for Meteor
https://packosphere.com/ostrio/flow-router-extra
BSD 3-Clause "New" or "Revised" License
201 stars 30 forks source link

Semantics of hooks and need for a pre-whileWaiting hook with redirect #96

Closed drone1 closed 7 months ago

drone1 commented 1 year ago

I'm having trouble understanding the semantics of the ordering of the 'hooks', which the wiki currently lists as the following:

.whileWaiting()
.waitOn()
.waitOnResources()
.endWaiting()
.data()
.onNoData()
.triggersEnter()
.action()
.triggersExit()

Specifically what I find confusing is that triggersEnter() functions takes a redirect parameter but is executed right before action(). I guess this is where redirects should occur since it takes a redirect argument. But why would we want to do all of this waiting and loading of resources and processing before we decide to redirect the user to another route? What is a concrete use case for this ordering?

Here's my simple use case:

Currently the best I can do, it seems, is do my checks on the user in whileWaiting which semantically makes zero sense, and since there is no redirect parameter, and since it's not stated in the docs, I'm not sure whether it's safe to use FlowRouter.go().

Am I misunderstanding something? What is the suggested approach for this basic use case?

It seems that in order to do this cleanly, one would need another hook: a pre-whileWaiting hook that takes a redirect parameter. Then one can do some basic checks to see if the route should be dealt with at all, like "is the user logged in?" and if not, redirect them to the login page.

Thoughts? Thanks a lot.

[EDIT: By the way, at least in 3.8.0 (current version is 3.9.0 at the time of this writing), whileWaiting() is called after waitOn, not before, as the wiki states]

drone1 commented 1 year ago

Anyone? :)

dr-dimitru commented 1 year ago

Hello @drone1

Missed that one, sorry. Thank you for the ping.

Specifically what I find confusing is that triggersEnter() functions takes a redirect parameter but is executed right before action(). I guess this is where redirects should occur since it takes a redirect argument. But why would we want to do all of this waiting and loading of resources and processing before we decide to redirect the user to another route? What is a concrete use case for this ordering?

.triggersEnter and . triggersExit is the heritage of the original flow-router. It was there and remains there for the legacy reasons, also it's still a good for the redirect as all hooks above it made in order to make a decision about redirection

Here's my simple use case:

I have a route which should only be displayed for logged-in-users If the user is logged in, I'd like to use FlowRouter's hooks to display a spinner (whileWaiting) and load private endpoint data for the user (waitOn), then render the page (action) If the user is not logged in, redirect to login page

We solve it in the other way — render login form instead of template when user not yet logged in, — it solves a lot of headaches about redirect before and after login. When user logs in, — the login form gets replaced with page's template, no navigation or browser reloads involved, use .refresh() or simply .reload() to re-run hooks. .refresh() documentation slightly cover your case with login logic examples

Hope that helps, lmk

dr-dimitru commented 7 months ago

Feel free to reopen it in case if the issue is still persists on your end.