Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cookie in nuxtServerInit()?

I am using vue-cookie package which lets me to set and get cookies with ease. What I want is to get this cookie in nuxtServerInit():

async nuxtServerInit() {
   const res = await this.$axios.post('/me', {}, {
       headers: {
          'Authorization': 'Bearer ' + $nuxt.$cookie.get('token')
       }
   })
}

But, I always get $nuxt is not defined error. Please help!

like image 545
Damon Avatar asked Dec 30 '25 03:12

Damon


1 Answers

vue cookie is a wrapper around tiny-cookie . Tiny cookie is for browser. So it wont work on server e.g. in nuxtServerInit

In nuxtServerInit you should get cookies from req.cookies

async nuxtServerInit(_, { req }) {
   console.log(req.headers.cookie)
}
like image 135
Aldarund Avatar answered Jan 01 '26 19:01

Aldarund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!