Skip to content

kvchekuri/Webassignment2

Repository files navigation

Assignment 2 Backend - Bookings API

This is a Node.js + Express + MongoDB backend for Assignment 2, implementing two core booking endpoints.

Setup

  1. Clone the repo or copy files
  2. Install dependencies:
    npm install
    
  3. Set up your .env file:
    MONGODB_URI=mongodb://localhost:27017/assignment2
    JWT_SECRET=your_jwt_secret_here
    PORT=5000
    
  4. Start MongoDB locally (or use MongoDB Atlas)
  5. Run the server:
    npm run dev
    
    or
    npm start
    

Authentication Endpoints

1. POST /api/auth/register

  • Purpose: Register a new user
  • Body:
    {
      "email": "[email protected]",
      "password": "Test123!@#",
      "firstName": "John",
      "lastName": "Doe",
      "role": "user"
    }
  • Success Response:
    {
      "message": "User registered successfully",
      "token": "<jwt_token>",
      "user": {
        "id": "<user_id>",
        "email": "[email protected]",
        "firstName": "John",
        "lastName": "Doe",
        "role": "user"
      }
    }

2. POST /api/auth/login

  • Purpose: Login user
  • Body:
    {
      "email": "[email protected]",
      "password": "Test123!@#"
    }
  • Success Response:
    {
      "message": "Login successful",
      "token": "<jwt_token>",
      "user": {
        "id": "<user_id>",
        "email": "[email protected]",
        "firstName": "John",
        "lastName": "Doe",
        "role": "user"
      }
    }

Booking Endpoints (Require Authentication)

1. POST /api/bookings

  • Purpose: Book a seat for an event
  • Headers: Authorization: Bearer <jwt_token>
  • Body:
    {
      "eventId": "<event_id>",
      "userId": "<user_id>",
      "seatNumber": "B14",
      "paymentId": "pay_123456"
    }
  • Success Response:
    {
      "message": "Booking confirmed",
      "bookingId": "<booking_id>",
      "qrCode": "<base64-string>"
    }

2. GET /api/bookings/:userId

  • Purpose: Get all bookings for a user
  • Headers: Authorization: Bearer <jwt_token>
  • Success Response:
    [
      {
        "bookingId": "<booking_id>",
        "eventName": "ReactJS Conference",
        "seat": "B14",
        "eventDate": "2025-07-28",
        "qrCode": "<base64-string>"
      }
    ]

Security Features

Authentication

  • JWT-based authentication
  • Token expiration (24 hours)
  • Password hashing with bcrypt

Authorization

  • Role-based access control (user, organizer, admin)
  • Users can only access their own bookings
  • Middleware validation for all endpoints

Input Validation

  • Email format validation
  • Password strength validation (min 8 chars, 1 number, 1 special)
  • Name sanitization
  • MongoDB ObjectID validation
  • Required field validation

Notes

  • Make sure to create some Event documents in your database for testing.
  • You can use Postman to test the endpoints.
  • For deployment, use Render, Heroku, or AWS as required by your assignment.

About

To deploy endpoints in render

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published