Express 5 + TypeScript REST API with MongoDB (Mongoose) featuring user authentication with session tokens.
# Install dependencies
npm install
# Create .env file
cp .env.example .env
# Configure environment variables
PORT=3000
MONGO_URL=mongodb://localhost:27017/your-databasenpm run dev # Watch mode with hot reload
npm run build # Build to dist/
npm start # Build and run| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /auth/register |
Register new user | No |
| POST | /auth/login |
Login user | No |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /users |
Get all users | Yes |
| PATCH | /user/:id |
Update user | Yes (Owner) |
| DELETE | /user/:id |
Delete user | Yes (Owner) |
POST /auth/register
Content-Type: application/json
{
"username": "john",
"email": "john@example.com",
"password": "secret123"
}Response (200):
{
"_id": "...",
"username": "john",
"email": "john@example.com",
"createdAt": "...",
"updatedAt": "..."
}POST /auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "secret123"
}Response (200): Returns user object and sets MEHEDI-AUTH cookie for session.
PATCH /user/:id
Content-Type: application/json
Cookie: MEHEDI-AUTH=<session_token>
{
"username": "newname"
}DELETE /user/:id
Cookie: MEHEDI-AUTH=<session_token>src/
├── app.ts # Express app instance
├── index.ts # Server entry point (middleware, routes)
├── router/ # Route definitions by feature
├── controllers/ # Request handlers
├── db/ # Mongoose models + data access
├── middlewares/ # Auth middleware (isAuthenticated, isOwner)
└── helpers/ # Utility functions (crypto)
- Uses cookie-based session tokens
- Passwords hashed with HMAC-SHA256 + salt
- Protected routes require
MEHEDI-AUTHcookie - Owner-only routes verify user ID matches request
ISC