DEV Community

Discussion on: How to use Global Navigation Guards with Nuxt Middleware, and why you absolutely should NOT

Collapse
julpat profile image
julpat

Well :)
Its quite simple.. imagine that middleware is (!) router.beforeEach


export default function ({ store }) {
  if(store.state.isUserEditing) {
      if(confirm("Are you sure?")) {
        store.commit('userEditing', false);
        next();
      } else {
        next(false);
      }
  }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
minicatscb profile image
minicatsCB

Where does next() come from?
For me, even a simple code like this:

export default function() {
  next(false)
}
Enter fullscreen mode Exit fullscreen mode

throws an error saying: "Reference error: next is not defined".

Collapse
julpat profile image
julpat

Sorry, that was taken just as an example from the article, but agree, can be confusing.

In docs you can find this example of middleware/auth.js

export default function ({redirect, store}) {
    const isAuthenticated = store.state.auth.user ? true : false
    if (!isAuthenticated) {
      redirect({name: 'auth'})
    }
  }
Enter fullscreen mode Exit fullscreen mode