Mikhail Krivosheev
Mikhail Krivosheev

Reputation: 569

Access to LocaleStorage from Middleware in Universal mode

Such a situation:
As far as I understand, to access LocaleStorage from Middleware in Universal mode
is impossible (at least by default.)

Question:
Are there any solutions (modules possible) to solve such problems?
(if yes - what can you recommend first of all?)

Upvotes: 0

Views: 892

Answers (2)

Ilijanovic
Ilijanovic

Reputation: 14904

If you want to store something on the client side that is not gonna getting deleted after page refresh then use cookies. You can access cookies in the middleware:

The Nuxt context contains the property req and the cookies are attached to the header

export default function ({ req, store}) {
   let cookie = require('cookie');
   let cookies = req.headers.cookie;
   let parsedCookies = cookie.parse(cookies);
   console.log(parsedCookies);
}

In this example i use the package cookie to parse the cookies npm i cookie

Upvotes: 1

Atif Zia
Atif Zia

Reputation: 793

Localstorage in nuxt middleware is not accesible as it is used for client side only. However We can use cookies and can also access vuex store in milleware as 1.Using cookie

export default (context) => {
    if (!context.app.context.app.$cookies.get('loginState')) {
        return context.redirect('/login')
    }
}

2.Accessing store in middleware

export default function({ store, redirect }) {
  if (!store.getters.GET_LOGIN_STATE) {
    return redirect('/login')
  }
}

Here are possible solutions put your data in store or cookie

Upvotes: 1

Related Questions