Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js REST API

Express 5 + TypeScript REST API with MongoDB (Mongoose) featuring user authentication with session tokens.

Setup

# Install dependencies
npm install

# Create .env file
cp .env.example .env

# Configure environment variables
PORT=3000
MONGO_URL=mongodb://localhost:27017/your-database

Development

npm run dev      # Watch mode with hot reload
npm run build    # Build to dist/
npm start        # Build and run

API Endpoints

Authentication

Method Endpoint Description Auth Required
POST /auth/register Register new user No
POST /auth/login Login user No

Users

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)

Request/Response Examples

Register User

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": "..."
}

Login

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.

Update User

PATCH /user/:id
Content-Type: application/json
Cookie: MEHEDI-AUTH=<session_token>

{
  "username": "newname"
}

Delete User

DELETE /user/:id
Cookie: MEHEDI-AUTH=<session_token>

Project Structure

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)

Authentication

  • Uses cookie-based session tokens
  • Passwords hashed with HMAC-SHA256 + salt
  • Protected routes require MEHEDI-AUTH cookie
  • Owner-only routes verify user ID matches request

License

ISC

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages