Kumail Hunaid Profile picture
Jan 23 11 tweets 2 min read
Most engineers don’t understand how #authentication works. Here’s how it works in a few tweets, easy peasy! 👇
A user is authenticated if they carry with them a session token that is valid.

All of authenticating a user is boilerplate around this concept.
When a user registers, we save their username and password in a database.

We don’t store the password as-is because our database could get hacked. So we store a hash of it.
A hash is a one way cryptographic transformation of the password.

By using this transformation to store and match passwords, we can be sure that the passwords will never leak. Because we don’t have them anymore. We’ve got a hash.
On logging in, a user provides a username which can be public and their password. We hash it again, and compare it with the hash we stored on signup.

If it matches, we can be certain the user has the account password.
Sometimes this is not enough, so we ask users to confirm their email address too.

Once a user has confirmed their user & password. We now create a “session” for them.
A session is like going to the waterpark and wearing a band. It helps you go on all the rides.

But when you leave the waterpark (logout), the session is no longer active.
Logging a user in is as simple as handing them their temporary water park band. We call it a session token.

The user then wears it on their wrist. That’s storing the session token in a browser cookie.

It stays on the wrist until they leave. It’ll be sent with every request.
When the server gets a session token that is stored in the session table, VOILA! They can go on all the rides, they’re authenticated!

When they leave the park or logout. We just delete the session token from our database. That invalidates their session.
That’s authentication in a nutshell.

All the forms, login, register, forgot password, change password, etc

Just create or remove session tokens.

Or update the users password hash.
Let me know if you have any questions!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Kumail Hunaid

Kumail Hunaid Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(