Personal · backend · 2025Done
Contacts API
A REST API in Express and MongoDB where each user registers, logs in and manages their own contacts. Passwords are hashed with bcrypt, logins return a 15-minute JWT, and every contact is stamped with the user who created it.
Who wrote whatAll eight commits are mine, from January 2025, built while learning Express.
- Bearer token in the header
- Verify the JWT, attach the user
- Controller: find contacts by user_id
- Errors go to one handler with a status code
What it does
- POST /api/users/register and /api/users/login; GET /api/users/current returns the signed-in user.
- GET, POST, PUT and DELETE on /api/contacts, all behind the token check. Listing only returns the caller's own contacts, and update and delete refuse with 403 if the contact belongs to someone else.
- express-async-handler passes every thrown error to one error middleware, which turns the status code into a consistent JSON response.
What I'd fix, rereading it
- Fetching one contact by ID doesn't check the owner, so any signed-in user who knew an ID could read someone else's contact. List, update and delete do check; read should too.
- A request with no Authorization header never gets a response: the middleware only answers inside the “Bearer” branch.
- Deleting a missing contact sets 404 but doesn't stop, so it crashes on the next line instead.
- Update passes the request body straight to the database, so a caller could rewrite user_id. It should pick the allowed fields.
- Error responses include the stack trace, which a public API shouldn't send.
- The README mentions express-validator, but the code only checks that fields are present. Real validation is still to do.
Next
Fix the six issues above with a test for each, then rate-limit the login route.
Screenshots to come.
NextThoughtShare→