kriasoft/universal-router

next(false)

FredericHeem opened this issue · 2 comments

Is there a way to stop any middleware by calling next(false)?
Right now, calling next(false) will result in a Page Not Found exception, would it be possible to throw another kind of exception (Redirect ?)
This is a situation which occurs when redirecting to a login page when the user is not authenticated.
Let me know your thought.

Throwing an exception inside the action instead of calling next(false) makes my issue go away.

  • context.next() iterates through children routes and returns result or null.
  • context.next(true) iterates through remaining routes and returns result or throws an exception.
  • return anything other than null or undefined to stop iteration.
const middlewareRoute = {
  path: '/admin',
  children: [/* ... */]
  action(context) {
    if (!userAuthorized) {
      return 'stop' // not null/undefined
    }
    return context.next()
  }
}

Demo: https://jsfiddle.net/frenzzy/2nq9o896/