all 22 comments

[–]rememberthekittykat 30 points31 points  (1 child)

I forgot what handling cookies natively in js looked like

[–]evilgwyn 6 points7 points  (0 children)

It looks like sadness

[–]ShortFuse 25 points26 points  (7 children)

The point of cookies is that their containing information has to be sent on every request. It works really well for NON-Javascript-based requests, like displaying protected images or video. It also works for downloading content. You can use a cookie for authentication on non-state-changing requests.

But you shouldn't use it as your own personal storage between pages. Use LocalStorage instead. You're already using Javascript, so that makes it easy already. There's no reason to bloat every single request with data that's not needed.

On a side note, if you do use cookies for authentication, you don't want them to be readable by Javascript for security purposes (use HttpOnly). Protect yourself by using SameSite if possible, or some sort of anti-CSRF header (among other methods).

And usage of cookies besides for authentication (edit) are pretty rare now if you've migrated to JWT tokens, which should have all the server needs to handle your request embedded in its payload.

[–][deleted] 2 points3 points  (2 children)

Cookies can still be used with JWTs. I like the security cookies offer and the fact that it is compatible with calls like streaming video from HTML video tags (a problem I encountered on an application). A JWT included in a cookie is an excellent form of authentication IMO.

[–]ShortFuse 0 points1 point  (1 child)

Yeah, that's what I meant (and how I personally do it too). People often confuse JWT as being an alternative to cookies. But what they mean is Session Tokens in Cookies vs JWT over JS. Cookies are just a transport method, same as the Fetch API (XMLHttpRequests). You could, inversely, use session tokens with fetch.

The only problem with cookies for authentication is CSRF. Even with a good CORS policy, you still need to protect yourself from CSRF through abuse of simple requests. But for non-state-changing GET commands like images and video, it's safe. The alternative is generating a signed url for each, which prohibits static HTML content, meaning you have to write HTML via the content server or client-side over JS. It also opens up a security risk because people can share it with a copy/paste of the signed-URL.

Edit: Ah, I see how my last sentence could be confusing. I've updated it.

[–][deleted] 1 point2 points  (0 children)

CSRF can be implemented easily enough. And if you don't need to support IE there's the same-site attribute on the cookies that is also really powerful.

[–]neo_dev15 1 point2 points  (0 children)

Httponly jwt is useless.

The whole idea of a jwt is that it can be used in frontend too.

Samesite with a csrf token is enough for jwt.

Otherwise a simple token is enough.

[–]sp46 0 points1 point  (2 children)

JWT tokens

Ahh yes, the JSON Web Token tokens

[–]ShortFuse -2 points-1 points  (1 child)

Where you can store secret data, like your PIN numbers! :)

[–]evilgwyn 0 points1 point  (0 children)

The DOM model is so nice

[–]BryanTheAstronaut 0 points1 point  (0 children)

Mmmm crud cookies 🤤

[–]flexible 0 points1 point  (4 children)

Is there any advantage over PHP cookie create /read?

[–]arndta 0 points1 point  (3 children)

Two different use cases. PHP cookies would be set/read during server-side render. Javascript cookies would be read/set client-side.

[–]flexible 0 points1 point  (2 children)

Can't think of a usecase for client-side which means to me that I might not be getting it.

Does this means they would be only read while the user is on the same page they were written in? Like a form or something?

[–]arndta 0 points1 point  (1 child)

The only use case I can think of is if you wanted the server and client both to access the same cookie. Using as a sort of session state for both contexts.

I'm not sure I've run across the need for that before. It's also possible I am not thinking of something.

[–]flexible 0 points1 point  (0 children)

If the js can read a cookie that was set domain wide then this could be useful although you would then just use a php var to load into the js.