Menu

I hardened my first Express auth API: Helmet, CORS and rate limiting
📰

I hardened my first Express auth API: Helmet, CORS and rate limiting

DEV Community: security·Felipe·10 days ago
#IEbmZyHb
#devtosecurity#devto#dev#enter#exit
Reading 0:00
15s threshold

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 ().…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.