onRoute hook
See original GitHub issueFor certain things like authenticated routes, it would be useful to have a function that runs before each route change so that authentication can be checked and the user can be redirected. Something like:
const app = choo({
onRoute (route, state, prev, send) {
if (route.auth && !state.authenticated) {
return send('location:setLocation', { location: '/login' }) //redirect
}
// otherwise default behavior
}
})
app.router((route) => [
route('/login', { auth: false }, loginView),
route('/protected', { auth: true }, protectedView)
])
app.start()
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:14 (10 by maintainers)
Top Results From Across the Web
Hooks - Fastify
By using hooks you can interact directly with the lifecycle of Fastify. ... To add more routes within an onRoute hook, the routes...
Read more >Hook onRoute doesn't work correct inside registered plugin
If the onRoute hook is in the main application code, then callback is called for all routes. And extraneous HEAD routes appear.
Read more >Server-Side Development with Fastify — App Hooks - Medium
onRoute — runs when a route is registered; onRegister — runs when a new plugin is registered and new encapsulation context is created....
Read more >Hooks - Fiber
OnRoute is a hook to execute user functions on each route registeration. Also you can get route properties by route parameter.
Read more >Hooks - Fastify
Hooks. Hooks are registered with the fastify.addHook method and allow you to listen to specific events in the application or request/response lifecycle.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Cool idea. Do you think it provides value over just wrapping your routes? ie.
@yoshuawuyts Sorry, ignore me. I just saw that
onRoute
is a feature of thechoo
function. I was confusing it with a model feature. It’s all clear now. Thanks.