Securing a small Next.js admin panel
The practical choices behind this site’s private dashboard: HTTP-only sessions, hashed passwords, validation and rate limits.
This portfolio has a private admin panel for writing posts, managing projects and reading messages. Here is how it is protected.
Sessions#
After sign-in the server issues a signed JWT stored in an HttpOnly, SameSite=Strict cookie that expires after eight hours. JavaScript in the browser can’t read it, and other sites can’t send it.
Passwords#
The admin password can be stored as a bcrypt hash, so even someone who reads the environment variables doesn’t learn the password itself.
Validation#
Every request body is validated with Zod before it touches the database. Anything with the wrong shape is rejected with a generic error.
Rate limiting#
The public contact form allows a handful of messages per IP address per hour, which stops casual spam without adding friction for real visitors.
Headers#
Middleware adds X-Frame-Options, X-Content-Type-Options, a strict referrer policy and a permissions policy to every response.
What’s next#
A managed rate limiter and bot protection on the contact form would be the next upgrades before heavy traffic.