title: Middleware on specific page - NuxtJS
body: <p>Well, I'm starting with nuxt and I have following routes:</p>
  
  <pre><code>/home
  
  /dashboard
  
  /login
  </code></pre>
  
  <p>I want to protect the <code>/dashboard</code>, but only for users logged in with a token in Cookie.</p>
  
  <p>Then i created a middleware</p>
  
  <pre><code>/middleware/auth.js
  
  import Cookie from 'js-cookie'
  
  export default function({ req, redirect }) {
    if (process.server) {
      if (!req.headers.cookie) return redirect('/login')
      const jwtCookie = req.headers.cookie.split(';').find(c =&gt; c.trim().startsWith('jwt='))
      if (!jwtCookie) return redirect('/login')
    } else {
      const jwt = Cookie.get('jwt')
      if (!jwt) { window.location = '/login' }
    }
  }
  </code></pre>
  
  <p>and register the middleware in my layout or dashboard page</p>
  
  <pre><code>&lt;script&gt;
  export default {
    middleware: 'auth',
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>when I access <code>/dashboard</code> apparently works perfectly</p>
  
  <p>but the problem is that the middleware is being registered globally, it is running on all pages, all routes</p>
  
  <p>So when you access <code>/home</code> that is a published page, if you do not have the cookie, you end up being redirected to login page</p>
  
  <p>anyone help?</p>