nuxt.jsnuxt3.js

Delete a Cookie in Nuxt.js 3


I'm trying to make a function that deletes a cookie when a button is clicked. I'm using the useCookie function from Nuxt 3. Since useCookie is provided by h3, I tried using deleteCookie (another function from h3), but that didn't work. I also tried setting the expire date to the past:

useCookie('userId', {
    expires: new Date().setDate(new Date().getDate() - 1)
})

but that didn't work either.


Solution

  • This is how useCookie works in Nuxt 3:

    To get cookie:

    const cookie = useCookie(name, options)
    

    To set cookie:

    cookie.value = YOUR_VALUE
    

    To remove cookie

    cookie.value = null