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 skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk