Rdd0u
Redirecting from middleware
Hi, the docs advise against returning a response in a middleware. https://h3.unjs.io/guide/event-handler#middleware
Middleware must not return any value or directly return a response for event. If you return a response, it will act as a normal event handler!
However, I need to redirect to a specific page if a user does not match some conditions. And this check needs to be performed on all possible server routes.
What are the possible risks/side effects of using a sendRedirect
in a middleware in that case? Or what would be the best to implement this?
Thanks,1 replies
Redirect from nitro middleware
Hi,
I am planning to build an app with nitro and some good old server side rendered html with pug templates enhanced with htmx.
As many apps I will have an onboarding flow and plan on maintaining a flag of the onboarding status in the session token.
If a user is trying to access a dashboard page whilst not being onboarded I want to redirect him to... the onboarding page.
A good place to do that would seem to be a middleware. However, h3/nitro docs mention
Middleware must not return any value or directly return a response for event.
So would using a sendRedirect()
if onboarded === false
in a middleware be bad practice?
If so, what would be the best to handle this use case? Adding to the context and verifying the flag in each handler seems.... counter productive.
Thanks1 replies