This is a small REST API built with TypeScript, Express, and MongoDB. I coded it to learn how REST APIs work and to include it in my portfolio.
- Practice backend fundamentals: routing, controllers, middlewares, and persistence
- Learn authentication flows and cookie-based sessions
- Build a clean, readable project a recruiter can scan in a few minutes
- Registers users and logs them in
- Stores users in MongoDB via Mongoose
- Protects routes with cookie-based auth
- Allows authenticated users to list, update, and delete users
- TypeScript
- Node.js + Express
- MongoDB + Mongoose
- Cookie-based sessions (HTTP cookie)
src/index.tsboots the Express app, adds middleware, and mounts the router.src/router/declares routes and maps them to controller handlers.src/controllers/receives the request, validates input, then calls the database layer.src/db/uses Mongoose models to query MongoDB.src/middlewares/runs before protected routes to enforce auth and ownership.
- Register:
- Accepts
email,username,password. - Password is salted and hashed.
- User is saved in MongoDB.
- Accepts
- Login:
- Looks up user by email and compares hashed password.
- Generates a session token.
- Stores the token in the user record.
- Sends the token back as the
APP-AUTHcookie.
- Protected routes:
isAuthenticatedreadsAPP-AUTHcookie.- The token maps to a user in MongoDB.
- The user record is attached to
req.identity.
- Ownership checks:
isOwnercomparesreq.identity._idto:idin the route.
- Uses HMAC with SHA-256 plus a random salt.
- A per-user salt is stored alongside the hash.
Base URL: http://localhost:8080
POST /auth/register- Create a userPOST /auth/login- Login and receiveAPP-AUTHcookieGET /users- List users (requires auth)PATCH /users/:id- Update a user (requires auth + owner)DELETE /users/:id- Delete a user (requires auth + owner)
- Install dependencies
npm install- Configure environment variables
cp .env.example .envOpen .env and set MONGO_URL to your MongoDB connection string.
Do not commit real credentials; keep .env local.
- Start the server
npm start- Try it out Register a user:
curl -X POST http://localhost:8080/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","username":"test","password":"pass123"}'Login to get the auth cookie:
curl -i -X POST http://localhost:8080/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"pass123"}'Use the APP-AUTH cookie from the login response to access protected routes:
curl http://localhost:8080/users \
-H "Cookie: APP-AUTH=<your-token>"src/index.ts- Express app setup and MongoDB connectionsrc/router/- Route definitionssrc/controllers/- Request handlerssrc/db/- Mongoose models and queriessrc/middlewares/- Auth and ownership checkssrc/helpers/- Hashing utilities
- Auth is cookie-based with a session token stored in MongoDB.
- CORS is configured for
http://localhost:8080.