question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

For 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
timwiscommented, Jul 10, 2016

Cool idea. Do you think it provides value over just wrapping your routes? ie.

const auth = (view) => (state, prev, send) => {
  return state.authenticated ? view(state, prev, send) : loginView(state, prev, send)
}

app.router((route => [
  route('/', homeView),
  route('/account', auth(accountView)),
  route('/purchases', auth(purchasesView))
])
1reaction
mantonicommented, Jul 12, 2016

@yoshuawuyts Sorry, ignore me. I just saw that onRoute is a feature of the choo function. I was confusing it with a model feature. It’s all clear now. Thanks.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found