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 working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.