I started auth-api to stop following tutorials and build something of my own. It's a small authentication API (register, login and a protected route) built with Node, Express, Drizzle ORM and MySQL.
For a while I only cared that it worked. Routes returned JSON, users got saved, great. Then I started wondering what happens when someone points a script at it. So I spent a few days adding the basic security layers, and this post is what I did and what I learned.
It's not finished. I'll tell you what's still missing at the end.
Repo: github.com/Fredericrypto/auth-api
First: validation and password hashing
Before any network stuff, I wanted two things: never trust the request body, and never store a password in a way I'd be embarrassed about.
Validation is Zod. Every body goes through a schema before it touches the database:
import { z } from ' zod ' ;
const registerSchema = z . object ({ email : z . string (). email (), senha : z . string (). min ( 6 ), nome : z . string ().…