How to apply middleware to some pages in Nuxt.js

Keywords

  • Nuxt.js

Contents

  • 1. Apply middleware to all pages
  • 2. Specify a page which is not applied in middleware
  • 3. URL

Applying a middleware to some pages one by one is too dull and might be a cause of a bug.

Although I read this github issue, I can not understand clearly about the way of this title.

But I finally find out the way. I will show it.

Apply middleware to all pages

// nuxt.config.js
router: {
    middleware: 'authenticated',
},

Specify a page which is not applied in middleware

// middleware/authenticated.js
export default function({store, route, redirect}) {
  if (!store.state.authenticate.authenticated && route.fullPath !== '/login') {
    return redirect('/login');
  }
}

URL