DEV Community

Jochem Stoel
Jochem Stoel

Posted on

4

The right way to implement authentication using cookies, according to you.

The internet is full of opinions when it comes to implementing authentication and the use of sessions/cookies. We all agree that storing passwords in cookies or setting a value like admin = true is a very bad idea. We also have to deal with the prevention of XSS cookie stealing. (appending document.cookie to an <IMG> element src attribute or whatever)
In many cases we also want to prevent that a 'saved' cookie will successfully authenticate a different client or device when copied.

In your experience, what is the right way to handle authentication? In PHP, JavaScript or whatever language / framework you happen to prefer?

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (3)

Collapse
 
andrewlucker profile image
Andrew Lucker

I've seen more services using localStorage or progressive webapp magic for sessions. I think this will become more popular.

I have a hard time imagining a post-Javascript internet that still uses cookies.

Collapse
 
ben profile image
Ben Halpern

Would that be a token stored in localStorage that is used for token-based authentication via ajax?

Collapse
 
andrewlucker profile image
Andrew Lucker

Sure if you need secure access to user information from the server. Alternatively local storage can store or cache data that doesn't need to be secret.

So for example, authenticated user actions might require a token, but some information from secondary indexes might get stored client side to improve performance/reduce lookups.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay