Auth: Cannot read property ‘$cookies’ of undefined 

I faced this problem while developing Nuxt.js authentication middleware.

The key this doesn’t work within the middleware, you need to replace it with app instead, take a look at the following example:

export default function ({ redirect, route, app }) {
    if (!app.$cookies.get(process.env.ACCESS_TOKEN_KEY)) {
      const path = encodeURIComponent(route.path)
      return redirect(`/login?r=${path}`)
    }
  }

Hope this code snippet will save your time 🙂

Author: Danyal
I'm a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions. I have a strong focus on API design and development, complemented by experience in web server setup and maintenance. My versatile expertise ensures seamless creation and maintenance of web applications, covering everything from intuitive user interfaces to robust server-side functionality. Passionate about coding and driven by a lifelong learning mindset, I invite you to explore more at danyal.dk.