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 🙂