I am trying to implement an authentication system and I've run into some problems and after a lot of troubleshooting I've come to realise that I don't fully understand the difference between theese three:
res.cookie(name, value, [options])
and
res.setHeader('Set-Cookie')
and
Cookies.set(name, value, [options]) //js-cookie npm package
I'm assuming that
res.cookie
(andres.setHeader
) are coming fromexpress
.Documentation for res.cookie states (just underneath the property table) that
So,
res.cookie
is just a wrapper aroundres.setHeader
to make the code clearer: you can pass options as an object, instead of manually constructing a header value.Both of those are called on a
res
(a.k.a response) object, so it's a serverside tool to tell the client "please put that into cookies".As for the last one,
Cookies.set
, that is intended to be run on the client instead. Internally, it just sets thedocument.cookie
propery.So, if we build a tl;dr table of all mentioned cookie setting options, here they are:
res.cookie
res.setHeader
Cookies.set
document.cookie =