-- online Search!

Accounts and Authentication

by kirupa   | filed under Build Essentials

TL;DR

When someone signs in, a site checks that they are who they claim to be, gives their browser a long random ID so it remembers them from page to page, and checks what they may do on every request. Those jobs are called authentication, sessions, and authorization, and a well-tested provider or library should handle most of them.

  • Never store passwords. Store a slow, salted hash made with Argon2id, scrypt, or bcrypt, so a leaked table doesn't hand anyone a working password.
  • A session is a long random ID in an HttpOnly, Secure, SameSite cookie. Signing out or resetting a password deletes it on the server.
  • Sign in with Google lets Google vouch for who someone is, and a passkey keeps its private key on the person's device, where a phishing site can't use it.
  • Rolling your own sign-in means getting a long list of details right. Use a provider or your framework's library, and check roles on your own server.

Lots of our regulars at Crumb & Co. order the same thing every week, and they keep asking if our website can just remember it. Wren asked the coding agent to add accounts, and minutes later, it cheerfully came back with a sign-up page, a sign-in page, and a users table with a column called password. It even offered to write the password reset emails next!

That one column made Wren nervous, and for good reason. Signing in looks like one small form, but the form is only the part we can see. We'll look at what really happens when someone signs in, how our site remembers them, and why a well-tested provider or library should do most of this work for us.

Onwards!

A Bouncer, a Wristband, and a List

Our regulars want to reorder their usual in one tap, and our staff want an admin page where Rosa (our shift lead) edits pickup slots and Odette (who runs the kitchen) issues refunds when a bake goes wrong.

Picture a busy club. A bouncer at the front door checks your ID, and once you're in, you get a wristband so nobody checks your ID again at every room. Further inside, a list says which back rooms each wristband can enter:

Three steps at a club, joined by arrows. 1. Check the ID: a bouncer in sunglasses stands by the front door holding someone's ID card. 2. Wristband on: a paper wristband with a printed tag that reads k7Q2. 3. Check the list: two back-room doors, VIP and Staff, and a list that says wristband k7Q2 may enter VIP but not Staff.

Our website is going to need each of these. Checking that someone is who they claim to be is called authentication, the wristband that remembers them is a session, and deciding what a signed-in person may do is called authorization. For our bakery, that list looks like this:

A table of who may do what. Columns: reorder my usual, edit pickup slots, and issue refunds. Customer (our regulars) may only reorder their usual. Shift lead (Rosa) may only edit pickup slots. Kitchen lead (Odette) may edit pickup slots and issue refunds.

Instead of listing permissions person by person, we give each person a named bundle of them called a role. A new shift lead will just get the shift lead role.

Never Keep the Password

Here is the users table our agent made, after our first four people sign up:

The users table with four rows of id, email, and password: 1, rosa@crumbandco.example, pieday1122. 2, odette@crumbandco.example, ovenmitt77. 3, cinnamon.fan@example.com, sourdough4life. 4, bagel.boss@example.com, sourdough4life. The password column is tinted red, and a handwritten note points at it: plain as day!

If this table ever leaks, whoever has it can sign in as anyone on it. Since a lot of people reuse passwords, those pairs may open their email too.

The fix is to never store the password. We'll run it through a one-way scramble that always turns the same input into the same jumble and can't be run backwards:

The password sourdough4life goes into a one-way scramble and comes out as 8196d16f7c and more characters. Below, an arrow from that jumble back to sourdough4life is crossed out with a red X, and a handwritten note points at the X: no way back!

This scramble is called a hash (our Hash Functions Deep Dive covers how they work). When someone signs in, we hash what they typed and compare it to our stored hash, and a match means the password was right.

There is a catch. Customers 3 and 4 both picked sourdough4life, so they'd get the same hash, and attackers keep huge precomputed lists of hashes for common passwords. Before hashing, we'll mix in a random value that's different for every account and store it next to the result. That value is called a salt:

Two customers typed the same password, sourdough4life. The first gets the random salt Qx7fT2kA and we store 51194991f2 and more characters. The second gets the salt 3mPzW9eB and we store f2fad155c1 and more characters. A handwritten note points at the stored values: totally different!

Now nobody can tell those two passwords match, and precomputed lists are useless, because every salt would need its own list.

The last ingredient is slowness. Everyday hashes like SHA-256 are built to be fast, which is great for checking a download and bad for passwords, because whoever steals our table gets to test guesses as fast as their hardware allows:

Two panels. Rosa signs in: her password pieday1122 goes through one slow hash, shown as one hourglass, and matches. With our stolen table: an attacker's guesses, 123456, password, qwerty, croissant1, sourdough4life, and the rest of the list, and every guess has its own hourglass.

Rosa waits on one slow hash when she signs in...and never notices. An attacker waits on every guess. The OWASP Password Storage Cheat Sheet recommends Argon2id, then scrypt, then bcrypt for older systems, and the usual libraries for all three make and store the salt for us.

Better Password Rules

Sign-up rules have changed too. NIST, the US agency whose digital identity guidelines much of the industry follows, finalized SP 800-63B-4 in July 2025, and it asks for this:

  • At least 15 characters for a password used by itself, or 8 when it's one step of a multi-step sign-in.
  • No rules like one uppercase letter, one number, and one symbol.
  • No forced changes every few months, only when a password has leaked.
  • A check of every new password against lists of common and breached passwords.

At the time of writing, Pwned Passwords has seen 12345 in data breaches more than 31 million times, which would not surprise President Skroob from Spaceballs, who learned that Planet Druidia's air shield used 1-2-3-4-5, the same combination he had on his luggage:

A gag. A blue suitcase with a combination lock set to 1 2 3 4 5, and a handwritten note pointing at the lock: same as my luggage!

Our sign-up form will turn 12345 away and tell the person why. Sorry, Mr. President.

Handing Out the Wristband

Once Rosa's password checks out, how does our server know it's still her on the next page? The web has no memory between requests, so we'll make up a long random ID right after she signs in, save it in our sessions table next to her account, and hand it to her browser in a cookie:

Rosa's browser sends her email and password to the Crumb & Co. server. The server adds a row to its sessions table, id k7Q2 for user rosa, expiring Dec 15, and answers with Set-Cookie: session=k7Q2. Rosa's browser shows Hi, Rosa! and keeps the cookie, and a handwritten note points at it: our wristband!

That cookie is Rosa's wristband, and her browser will send it back with every request so our server can look up who's asking:

Rosa's browser asks the server for /admin/slots and sends Cookie: session=k7Q2. The server finds that id in its sessions table, highlighted, and it belongs to rosa, the shift lead. The server answers with the pickup slots page, which shows three slots on Nov 24 with 12 pies each.

The ID means nothing by itself, since all of the meaning lives in our table. It just has to be unguessable, and the OWASP Session Management Cheat Sheet asks for at least 64 bits of randomness. Here is the full header we send:

Set-Cookie: session=k7Q2xV9pR3mT8wZ1bN6cYd4Hs0LqE5uJ; HttpOnly; Secure; SameSite=Lax; Path=/

The words after the ID make it a good wristband (MDN covers every option):

  • HttpOnly keeps JavaScript on the page from reading it, so a sneaky script can't steal it.
  • Secure sends it only over HTTPS.
  • SameSite=Lax leaves it off most requests that start on other sites, which helps block forged ones.

When Rosa signs out, we delete her row on our server, so any copy of her cookie, wherever it lives, stops working:

The row for session k7Q2 is crossed out in the server's sessions table, and a handwritten note points at it: deleted! The browser still sends Cookie: session=k7Q2 when it asks for /admin/slots, and the server answers with the sign-in page.

When Rosa changes her password, we'll delete all of her other sessions the same way.

Note: What About Tokens Like JWTs?

Some apps hand out a signed token, like a JSON Web Token (JWT), that carries the user's details inside it. It needs no lookup, but signing out can't cancel it before it expires without extra work, so a session ID is easier to take back.

When Someone Forgets Their Password

Our forgot-password page gives everyone the same answer, whether or not the email has an account:

Two forgot-password forms on crumbandco.example. On the left, someone types rosa@crumbandco.example, which has an account. On the right, someone types nobody@example.com, which doesn't. Both get the same green message: If that email has an account, we've sent a reset link. A handwritten note points at the right one: exactly the same!

Otherwise, anyone could use our page to find out who shops with us. The OWASP Forgot Password Cheat Sheet even wants the same response time either way. When the account exists, we'll send this:

An email from Crumb & Co. with the subject Reset your password. It says someone asked to reset the password for this account, and if it was you, use this link: crumbandco.example/reset?token=Zp4Lw8, with the token highlighted. The link works once and expires in 30 minutes. Beside the email, three notes about the token: long and random, works once, and expires in 30 minutes.

We'll store a hash of that token, just like a password. Once Rosa picks a new password, we'll sign her out everywhere and email her about the change, so anyone else in her account is out too.

Letting Google Do the Checking

Some regulars would rather not make another password, so let's add a Sign in with Google button. It works like a valet key, which starts a car but won't open the trunk. Google hands our bakery a limited pass with the customer's name and email, and never their password. The standard behind passes like this is OAuth 2.0, and OpenID Connect adds a signed note saying who the person is, called an ID token:

Three lanes: the customer's browser, the Crumb & Co. server, and Google. 1. The browser tells Crumb & Co. sign in with Google. 2. Crumb & Co. sends the browser to Google. 3. The customer signs in at Google and says yes to sharing their name and email. 4. Google gives the browser a one-time code. 5. The browser hands the code to Crumb & Co. 6. Crumb & Co. sends the code plus its own secret straight to Google. 7. Google answers with an ID token that says who this is. 8. Crumb & Co. gives the browser a session cookie. Steps 6 and 7 sit on a green band, because they happen directly between the two servers.

Look at steps 3 and 6. The customer types their password at Google, never on our site, and our server swaps the code for the ID token directly with Google, using a secret key like the ones in APIs and Webhooks. A good library checks that the token came from Google, was meant for us, and hasn't expired, and step 8 is our trusty wristband again.

Keys That Can't Be Phished

Passwords have one problem no hashing can fix: people can be tricked into typing them on a fake site. What if there were nothing to type? Instead, Rosa's phone makes a matched pair of keys. One stays locked on the phone and works only after she unlocks it with her fingerprint, face, or PIN. The other is safe to share, so our server stores it:

Rosa's phone holds her private key, a gold key in a locked box labeled stays on the phone, which unlocks with her fingerprint, face, or PIN. An arrow carries only the matching public key, a blue key, to the Crumb & Co. server, which stores it in a passkeys table next to the user rosa.

This pair is a passkey, made of a private key on Rosa's phone and a public key on our server. To sign in, we'll send her phone a random challenge, her phone will sign it, and our server will check the signature:

1. The Crumb & Co. server sends Rosa's phone a random challenge, x8Fq2Lm. 2. Rosa unlocks her phone, and it signs the challenge with her private key and sends back the signature, MEUCIQ. 3. The server checks the signature with Rosa's public key from its passkeys table. It matches, so it's Rosa.

A public key can check signatures but can't make them, so a leaked passkeys table won't let anyone sign in as Rosa. Each passkey also belongs to the one site it was made for, so a look-alike site gets nothing:

Two browser windows. On the real site, crumbandco.example, Rosa's phone offers her passkey: sign in as rosa?, passkey for crumbandco.example, with a Continue button. On a look-alike site, crumbandco-login.example, with its address tinted red, there's nothing to offer: no passkeys for this site. A handwritten note points at the look-alike site: look-alike!

Rosa doesn't have to spot the fake address, because her phone and browser check it for her. Passkeys are built on FIDO Alliance standards and Web Authentication (WebAuthn), a W3C standard. Like multi-factor sign-in (a password plus a second proof, like a code from an app), a passkey needs both Rosa's phone and the fingerprint, face, or PIN that unlocks it. Unlike a code, though, it can't be typed into a fake site.

Why We Shouldn't Build This Ourselves

Here is what our agent built, next to everything underneath it that also has to be right:

An iceberg. Above the water: the sign-in form and the users table, with a handwritten note pointing at the tip: what the agent built! Below the water, a much bigger part: slow, salted hashing; breached-password checks; limits on guessing; credential stuffing; constant-time checks; expiring reset tokens; the same reply for every email; email verification; signing out everywhere; multi-factor sign-in; passkeys; and account recovery.

Homemade sign-in breaks somewhere under that water. In credential stuffing, attackers try email and password pairs leaked from other sites on ours. A constant-time check takes the same time whether secrets match or not, so the timing leaks nothing. Writing our own versions of code like this is called rolling your own crypto, and the OWASP Authentication Cheat Sheet shows how much there is to get right.

Instead, we'll use an auth provider like Auth0, Clerk, or Firebase Authentication, or our framework's built-in auth library, since these have been attacked and patched for years. Providers have downsides, too. They cost more as we grow, switching later is real work, and when one goes down, nobody can sign in. A framework's library avoids most of that, but keeping it updated is on us.

Either way, one part stays ours. A provider can tell us this is Rosa, but it has no idea that she may edit pickup slots and can't issue refunds:

Two requests from Rosa's browser, each with Cookie: session=k7Q2: POST /admin/slots and POST /admin/refunds. The Crumb & Co. server looks up the session: rosa, shift lead. Edit pickup slots is allowed, so the first request gets 200 Saved. Issue refunds is not, so the second gets 403 Not allowed. A handwritten note points at the refunds rule: checked here!

Hiding the Refund button from Rosa is nice, but anyone can send a request straight to /admin/refunds, so the real check lives on our server:

app.post("/admin/refunds", requireSignIn, (req, res) => {
  if (!req.user.roles.includes("kitchen_lead")) {
    return res.status(403).send("Not allowed");
  }
  issueRefund(req.body.orderId, req.body.amount);
  res.send("Refund issued");
});

Our provider or library gives us requireSignIn, which reads our session cookie and fills in req.user. The role check after it is ours to write.

When an agent offers to write sign-in from scratch, ask it to use a provider or our framework's auth library, and to show you where each admin route checks roles on the server. Rye, our back-office agent, gets its own account with only the permissions it needs, and it never borrows Odette's password.

Try It This Week

Grab a test app, and let's add sign-in the boring, safe way:

  1. Add sign-in with an auth provider or your framework's auth library, and tell any agent helping you not to write its own password code.
  2. Sign in on two browsers, like your laptop and your phone.
  3. Sign out on one. Its back button shouldn't show private pages, and the other browser should still work.
  4. Reset your password (or create a passkey), and see what happens to the other browser's session.
  5. Sign in as a non-admin and type an admin URL into the address bar. The server itself should answer with a 403.

Frequently Asked Questions

What Is the Difference Between Authentication and Authorization?

Authentication checks that someone is who they claim to be, like a bouncer checking an ID at the door. Authorization decides what that signed-in person is allowed to do, like a list of which back rooms each wristband can enter. A sign-in provider can handle authentication for us, but rules like who may issue refunds are ours to check on the server.

How Should Passwords Be Stored?

Never store a password the way it was typed. Store a slow, salted hash instead, made with Argon2id, scrypt, or bcrypt, as the OWASP Password Storage Cheat Sheet recommends. The salt makes the same password produce different stored values, and the slowness makes every guess expensive for anyone who steals the table.

What Is a Passkey and Why Is It Safer Than a Password?

A passkey is a matched pair of keys made for one website. The private key stays on the person’s device and only works after they unlock it with a fingerprint, face, or PIN, while the website stores just the public key. There’s nothing to type, nothing useful to steal from the website, and a look-alike phishing site can’t use the passkey because it was made for the real site.

Should You Build Your Own Login System?

Usually not. Secure sign-in means getting a long list of details right, including password hashing, rate limits, reset tokens, session revocation, multi-factor sign-in, and account recovery. Use an auth provider or your framework’s well-tested library, and spend your effort on checking roles and permissions on your own server.

Conclusion

Phew! What looked like one small form turned out to be a bunch of separate jobs. Authentication checks who someone is, a session is the wristband that remembers them, and authorization decides what they can do. Behind them sit slow, salted hashes, reset links that work once, and Google or a passkey doing the checking. No provider can write our rules, though, so the role checks on our server are still on us.

As for Crumb & Co., our regulars now reorder in one tap, and Wren's agent never got to write those reset emails. It seemed a little sad, so Wren let it write the Your croissants are ready! emails instead.

Before you go, here are a few things you can do:

  1. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--