Accent color
All projects

Sign-up and sign-in API

Secure API

Topics
Backend, Security
Built with
  • Node.js
  • Express
  • Sequelize
  • JWT
  • Cookies
A sign-in request answered with an HttpOnly session cookie, and a tampered request answered with a plain 401.

This project was about building login end to end without cutting corners. Users sign up with a validated email and password, the password is salted and hashed before it ever reaches the database, and Sequelize handles the models, so no query is ever glued together from strings.

Signing in issues a JWT and sets it as an HttpOnly cookie, out of reach of page scripts. Every protected route runs the same middleware: read the token, verify its signature and expiry, and load the user it belongs to. Any failure along the way gets the same 401, so a probe can’t tell which check it tripped.

The source code didn’t survive, so there’s nothing to download, but it’s the design I still reach for whenever something needs accounts.

Three paths, one rulebook.

Sign up, sign in, and every request after that. Each path does its checks in a fixed order and gives up at the first one that fails.

Three lanes: sign up validates, checks the email, hashes and creates the user; sign in finds, compares, signs a JWT and sets the cookie; protected routes read, verify and load the user, with one 401 for any failure.

Why it’s built this way.

The token lives in an HttpOnly cookie rather than local storage, so a script injected into the page can’t read it. Passwords are only ever compared as hashes, so a leaked table doesn’t hand out logins.

Loading the user on every request means a deleted account stops working at once, even with a token that hasn’t expired. And because every failure answers the same way, there’s no difference between ‘bad signature’ and ‘no such user’ for anyone to measure.

  • Validation before any database work
  • Parameterized queries through the ORM
  • One 401 for every kind of no