matthewmueller / next-cookies

Tiny little function for getting cookies on both client & server with next.js.
368 stars 17 forks source link

How to Write, Delete #17

Closed mehrdad-shokri closed 5 years ago

mehrdad-shokri commented 5 years ago

The README provides an example for reading. const { token } = cookies(ctx) how to write or delete a cookie?

coder054 commented 5 years ago

write: document.cookie = "token=whatever"

mehrdad-shokri commented 5 years ago

Will this not remove other present cookies?

nfriedly commented 5 years ago

Nope, cookies are wierd. Setting document.cookie will either add a new cookie or else overwrite an existing one if it has the same name (and path and domain...)

You probably want to set path=/ and domain=.your domain.com to avoid having duplicates. And possibly the expiration date to make it last longer.

To delete a cookie, do the same thing but set the expiration date in the past.

This library only supports reading cookies. You can use the browser's built-in mechanisms for writing and deleting, or choose another library that does everything.

mehrdad-shokri commented 5 years ago

@nfriedly yeah, I ended up using js-cookie alongside this project. Thank you