Skip to content

Xabhi0811/Oline-Taxi-App

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

101 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— Uber App - Complete Documentation

A full-stack ride-sharing application built with Node.js/Express backend and React/Vite frontend, featuring real-time location tracking and ride management using WebSocket technology.


๐Ÿ“‹ Table of Contents

  1. Project Overview
  2. Tech Stack
  3. Project Structure
  4. Installation & Setup
  5. Environment Variables
  6. Running the Project
  7. Project Flow
  8. API Endpoints
  9. Socket.IO Events
  10. Database Schema
  11. Frontend Components
  12. Key Features

๐ŸŽฏ Project Overview

This Uber-like application is a two-sided marketplace that connects:

  • Users: People looking for rides
  • Captains: Drivers offering rides

Core Functionality:

  • User and Captain authentication (Register/Login/Logout)
  • Real-time ride booking and management
  • Live location tracking using Google Maps API
  • Fare calculation based on distance
  • Real-time communication using WebSocket (Socket.IO)
  • Ride confirmation and completion workflow

๐Ÿ› ๏ธ Tech Stack

Backend

  • Runtime: Node.js
  • Framework: Express.js v5.1.0
  • Database: MongoDB (Mongoose ODM v8.16.1)
  • Authentication: JWT (JSON Web Tokens)
  • Password Hashing: bcrypt/bcryptjs
  • Real-time: Socket.IO v4.8.1
  • API Validation: express-validator
  • CORS: Enabled for frontend communication
  • Environment: dotenv for configuration

Frontend

  • UI Framework: React 19.1.0
  • Build Tool: Vite 7.0.3
  • Routing: React Router v7.6.3
  • Styling: Tailwind CSS v4.1.11
  • Icons: Remix Icon v4.6.0
  • Animations: GSAP v3.13.0
  • HTTP Client: Axios
  • Real-time: Socket.IO Client v4.8.1
  • CSS: Custom CSS + Tailwind

๐Ÿ“ Project Structure

uber-app/
โ”œโ”€โ”€ Backend/                          # Node.js/Express Server
โ”‚   โ”œโ”€โ”€ app.js                        # Express app setup & routes
โ”‚   โ”œโ”€โ”€ server.js                     # Server startup & Socket.IO initialization
โ”‚   โ”œโ”€โ”€ socket.js                     # Socket.IO configuration & event handlers
โ”‚   โ”œโ”€โ”€ package.json                  # Backend dependencies
โ”‚   โ”œโ”€โ”€ .env                          # Environment variables
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ controllers/                  # Business logic
โ”‚   โ”‚   โ”œโ”€โ”€ user.controller.js        # User registration, login, logout, profile
โ”‚   โ”‚   โ”œโ”€โ”€ captain.controller.js     # Captain registration, login, profile
โ”‚   โ”‚   โ”œโ”€โ”€ ride.controller.js        # Ride creation, confirmation, completion
โ”‚   โ”‚   โ””โ”€โ”€ map.controller.js         # Maps & location-related endpoints
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ services/                     # Business logic & database operations
โ”‚   โ”‚   โ”œโ”€โ”€ user.service.js           # User CRUD operations
โ”‚   โ”‚   โ”œโ”€โ”€ captain.service.js        # Captain CRUD operations
โ”‚   โ”‚   โ”œโ”€โ”€ ride.service.js           # Ride logic (create, confirm, complete)
โ”‚   โ”‚   โ”œโ”€โ”€ map.service.js            # Google Maps integration & distance calc
โ”‚   โ”‚   โ””โ”€โ”€ fare.util.js              # Fare calculation logic
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ models/                       # Database schemas
โ”‚   โ”‚   โ”œโ”€โ”€ user.model.js             # User schema with auth methods
โ”‚   โ”‚   โ”œโ”€โ”€ captain.module.js         # Captain schema with auth methods
โ”‚   โ”‚   โ”œโ”€โ”€ ride.module.js            # Ride schema with status tracking
โ”‚   โ”‚   โ”œโ”€โ”€ blacklistToken.model.js   # Token blacklist for logout
โ”‚   โ”‚   โ””โ”€โ”€ middlewares/
โ”‚   โ”‚       โ””โ”€โ”€ auth.middleware.js    # JWT verification middleware
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ routes/                       # API endpoints
โ”‚   โ”‚   โ”œโ”€โ”€ user.routes.js            # /users/* endpoints
โ”‚   โ”‚   โ”œโ”€โ”€ captain.routes.js         # /captains/* endpoints
โ”‚   โ”‚   โ”œโ”€โ”€ ride.routes.js            # /rides/* endpoints
โ”‚   โ”‚   โ””โ”€โ”€ map.routes.js             # /map/* endpoints
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ db/
โ”‚       โ””โ”€โ”€ db.js                     # MongoDB connection setup
โ”‚
โ”œโ”€โ”€ Frontend/                         # React/Vite Application
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ main.jsx                  # React entry point with providers
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx                   # Main app router
โ”‚   โ”‚   โ”œโ”€โ”€ App.css                   # Global styles
โ”‚   โ”‚   โ”œโ”€โ”€ index.css                 # Base styles
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”œโ”€โ”€ context/                  # Global state management
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserContext.jsx       # User state & auth
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CaptainContext.jsx    # Captain state & auth
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ SocketContext.jsx     # Socket.IO connection & events
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”œโ”€โ”€ Pages/                    # Full page components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Start.jsx             # Landing/Home page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserLogin.jsx         # User login page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserSignup.jsx        # User registration page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserLogout.jsx        # User logout
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserProtectWrapper.jsx # Protected route wrapper for users
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Home.jsx              # User home (search & booking)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Riding.jsx            # User riding status page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CaptainLogin.jsx      # Captain login page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CaptainSignup.jsx     # Captain registration page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CaptainProtectWrapper.jsx # Protected route wrapper for captains
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CaptainHome.jsx       # Captain home (available rides)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ CaptainRiding.jsx     # Captain riding status page
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ test.jsx              # Test component
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”œโ”€โ”€ componets/                # Reusable UI components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ LocationSearchPanel.jsx    # Location input & suggestions
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ VehiclePanel.jsx          # Vehicle type selection
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ConfirmRidePopUp.jsx      # Ride confirmation popup
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ConfirRide.jsx            # Confirm ride details
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ LookingForDriver.jsx      # Searching for driver
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ RidePopUp.jsx             # Ride available notification
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ WaitingForDriver.jsx      # Waiting for driver to arrive
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ FinishRide.jsx            # Ride completion component
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ CaptainDetails.jsx        # Captain info display
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ””โ”€โ”€ assets/                   # Images, icons, static files
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ package.json                  # Frontend dependencies
โ”‚   โ”œโ”€โ”€ vite.config.js                # Vite configuration
โ”‚   โ”œโ”€โ”€ eslint.config.js              # ESLint rules
โ”‚   โ”œโ”€โ”€ index.html                    # HTML entry point
โ”‚   โ””โ”€โ”€ public/                       # Static assets
โ”‚
โ””โ”€โ”€ package.json                      # Root package (axios)

๐Ÿš€ Installation & Setup

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or Atlas)
  • Google Maps API Key
  • npm or yarn

Step 1: Clone & Navigate

cd uber-app

Step 2: Backend Setup

cd Backend
npm install

Step 3: Frontend Setup

cd ../Frontend
npm install

Step 4: Configure Environment Variables

Create a .env file in the Backend/ directory (see Environment Variables section)


๐Ÿ” Environment Variables

Create Backend/.env with the following variables:

# Server Port
PORT=4000

# MongoDB Connection String
DB_CONNECT=mongodb:
# OR use MongoDB Atlas:
# DB_CONNECT=mongodb+srv:o

# JWT Secret (used for token signing)
JWT_SECRET=

# Google Maps API Key
GOOGLE_MAPS_API=

Important Notes:

  • JWT_SECRET: Should be a strong, random string in production
  • DB_CONNECT: MongoDB connection URL
  • GOOGLE_MAPS_API: Get from Google Cloud Console
  • PORT: Default is 4000

โ–ถ๏ธ Running the Project

Terminal 1: Start MongoDB

# If running locally
mongod

# OR use MongoDB Atlas (no local server needed)

Terminal 2: Start Backend Server

cd Backend
npm install  # if not done
node server.js

Expected output:

Server is running on port 4000
[Socket] โœ… Socket.IO initialized

Terminal 3: Start Frontend Development Server

cd Frontend
npm install  # if not done
npm run dev

Expected output:

VITE v7.0.3 ready in XXX ms
โžœ  Local:   http://localhost:5173/

Access the App


๐Ÿ”„ Project Flow

1๏ธโƒฃ User Registration & Authentication Flow

User Signup Page โ†’ Register (POST /users/register)
    โ†“
Backend validates input & hashes password
    โ†“
Store user in MongoDB
    โ†“
Generate JWT token โ†’ Return token to frontend
    โ†“
Token stored in cookies/localStorage
    โ†“
User authenticated โœ…

2๏ธโƒฃ Ride Request Flow (User Perspective)

User Login โ†’ Home Page
    โ†“
Enter Pickup Location โ†’ Enter Destination
    โ†“
Select Vehicle Type (Car/Bike/Auto)
    โ†“
System calculates FARE (GET /rides/fare)
    โ†“
User confirms ride (POST /rides/create)
    โ†“
Backend finds nearby captains (within 5km radius)
    โ†“
Socket.IO sends "new-ride" event to available captains
    โ†“
User sees "Looking for Driver" screen
    โ†“
Waiting for captain acceptance...

3๏ธโƒฃ Ride Acceptance Flow (Captain Perspective)

Captain logs in โ†’ Captain Home Page
    โ†“
Receives "new-ride" notification via Socket.IO
    โ†“
Views ride details (Pickup, Destination, Passenger name)
    โ†“
Captain can accept or reject the ride
    โ†“
If accepted: Send ride confirmation (POST /rides/confirm)
    โ†“
Socket.IO notifies user: "ride-confirmed"
    โ†“
Both user & captain see real-time location updates

4๏ธโƒฃ Real-Time Location Tracking

Captain Starts Driving
    โ†“
Socket.IO event "update-location-captain" fired
    โ†“
Location (lat, lng) sent to backend
    โ†“
Backend broadcasts location to user via Socket.IO
    โ†“
User sees captain's real-time location on map
    โ†“
Captain arrives โ†’ Pickup passenger
    โ†“
Ride starts โ†’ Destination tracking begins

5๏ธโƒฃ Ride Completion Flow

Captain reaches destination
    โ†“
User confirms arrival
    โ†“
Ride marked as "completed"
    โ†“
Fare charged to user account
    โ†“
Rating & feedback (optional)
    โ†“
Ride history updated for both parties

๐Ÿ“ก API Endpoints

User Endpoints (/users/)

Method Endpoint Description Auth Required
POST /users/register Register new user โŒ
POST /users/login Login user โŒ
GET /users/profile Get user profile โœ…
POST /users/logout Logout user โœ…

Register Request:

{
  "fullName": {
    "firstName": "John",
    "lastName": "Doe"
  },
  "email": "john@example.com",
  "password": "password123"
}

Login Request:

{
  "email": "john@example.com",
  "password": "password123"
}

Captain Endpoints (/captains/)

Method Endpoint Description Auth Required
POST /captains/register Register new captain โŒ
POST /captains/login Login captain โŒ
GET /captains/profile Get captain profile โœ…

Ride Endpoints (/rides/)

Method Endpoint Description Auth Required
POST /rides/create Create new ride request โœ…
GET /rides/fare Calculate fare โŒ
POST /rides/confirm Captain accepts ride โœ…
POST /rides/start Start ongoing ride โœ…
POST /rides/end Complete ride โœ…

Create Ride Request:

{
  "userId": "user_id_here",
  "pickup": "123 Main Street, City",
  "destination": "456 Oak Avenue, City",
  "vehicleType": "car"  // or "bike", "auto"
}

Map Endpoints (/map/)

Method Endpoint Description Auth Required
GET /map/get-coordinates Get coordinates for address โŒ
GET /map/distance-time Calculate distance & time โŒ
GET /map/get-suggestions Autocomplete suggestions โŒ

๐Ÿ”Œ Socket.IO Events

Client โ†’ Server Events

1. Update Socket ID

socket.emit('json', {
  userId: 'user_id',
  userType: 'user'  // or 'captain'
});

2. Update Captain Location

socket.emit('update-location-captain', {
  userId: 'captain_id',
  location: {
    lat: 40.7128,
    lng: -74.0060
  }
});

3. Captain Accept Ride

socket.emit('ride-accepted', {
  rideId: 'ride_id',
  captainId: 'captain_id'
});

4. Captain Start Ride

socket.emit('ride-started', {
  rideId: 'ride_id'
});

5. End Ride

socket.emit('ride-ended', {
  rideId: 'ride_id'
});

Server โ†’ Client Events

1. New Ride Available (Captain receives)

socket.on('new-ride', (data) => {
  // data contains ride details:
  // {
  //   _id: ride_id,
  //   user: { name, email, ...},
  //   pickup: location,
  //   destination: location,
  //   vehicleType: 'car',
  //   fare: amount,
  //   distance: km,
  //   duration: minutes
  // }
});

2. Ride Confirmed (User receives)

socket.on('ride-confirmed', (data) => {
  // data contains captain details and ride info
});

3. Location Updated (User receives)

socket.on('location-updated-user', (data) => {
  // Captain's real-time location
  // {
  //   lat: number,
  //   lng: number
  // }
});

4. Ride Started

socket.on('ride-started', (data) => {
  // Ride is now in progress
});

5. Ride Ended

socket.on('ride-ended', (data) => {
  // Ride completed
});

6. Error Event

socket.on('error abhi', (data) => {
  // Error handling
});

๐Ÿ—„๏ธ Database Schema

User Schema

{
  fullname: {
    firstname: String,    // min 3 chars, required
    lastname: String      // min 3 chars
  },
  email: String,          // unique, required, min 5 chars
  password: String,       // hashed, required, not selected by default
  socketID: String,       // For real-time communication
  createdAt: Date         // Auto-generated
}

User Methods:

  • generateAuthToken() - Creates JWT token
  • comparePassword(password) - Verifies password
  • hashPassword(password) - Static method to hash password

Captain Schema

{
  fullname: {
    firstname: String,
    lastname: String
  },
  email: String,          // unique, required
  password: String,       // hashed
  socketId: String,       // Real-time tracking
  vehicle: {
    color: String,
    plate: String,
    capacity: Number,
    vehicleType: String   // 'car', 'bike', 'auto'
  },
  location: {
    lat: Number,
    lng: Number
  },
  status: String,         // 'active', 'inactive'
  createdAt: Date
}

Ride Schema

{
  user: ObjectId,         // Reference to User
  captain: ObjectId,      // Reference to Captain (after acceptance)
  pickup: String,         // Pickup address
  destination: String,    // Destination address
  fare: Number,           // Calculated fare
  distance: Number,       // In km
  duration: Number,       // In minutes
  status: String,         // 'pending', 'accepted', 'completed', 'cancelled'
  vehicleType: String,    // 'car', 'bike', 'auto'
  Otp: String,           // One-time password for verification
  rating: Number,         // 1-5 stars
  createdAt: Date,
  updatedAt: Date
}

Blacklist Token Schema

{
  token: String,          // JWT token to blacklist
  createdAt: Date,        // Auto-expires after 24h
  expiresAt: Date
}

๐ŸŽจ Frontend Components

Pages

Page Path Description
Start / Landing page with login/signup options
User Login /login User authentication page
User Signup /signup User registration page
User Home /home Main page to search and book rides
Riding /riding Active ride status (user perspective)
Captain Login /captain-login Captain authentication
Captain Signup /captain-Signup Captain registration
Captain Home /captain-home Available rides for captain
Captain Riding /captain-riding Active ride (captain perspective)
Logout /users/logout Logout and redirect

Reusable Components

Component Purpose
LocationSearchPanel Location input with Google Places suggestions
VehiclePanel Vehicle type selection (Car/Bike/Auto)
ConfirmRidePopUp Popup to confirm ride details
ConfirmRide Final ride confirmation screen
LookingForDriver Loading screen while searching for driver
RidePopUp Notification when ride is available (captain)
WaitingForDriver User waiting for captain to arrive
FinishRide Ride completion and rating screen
CaptainDetails Display captain info on ride

Context (Global State)

UserContext

Manages user authentication state:

{
  user: { _id, email, fullname, ... },
  isLoading: Boolean,
  error: String,
  loggedIn: Boolean
}

CaptainContext

Manages captain authentication state:

{
  captain: { _id, email, fullname, vehicle, ... },
  isLoading: Boolean,
  error: String,
  loggedIn: Boolean
}

SocketContext

Manages Socket.IO connection:

{
  socket: SocketInstance,
  connected: Boolean,
  events: { ... }
}

โญ Key Features

1. Dual Authentication System

  • Separate login flows for Users and Captains
  • JWT-based authentication
  • Password hashing with bcrypt
  • Automatic token refresh

2. Real-Time Ride Matching

  • Find captains within 5km radius of pickup
  • Instant notifications via Socket.IO
  • Captain can accept or reject rides

3. Location-Based Services

  • Google Maps integration
  • Real-time location tracking
  • Distance & fare calculation
  • Address autocomplete suggestions

4. Live Location Updates

  • Captain location broadcast to user
  • User sees captain approaching in real-time
  • Multi-device support via Socket.IO

5. Ride Management

  • Create, confirm, start, and complete rides
  • OTP verification for ride confirmation
  • Ride history tracking
  • Status updates in real-time

6. Fare Calculation

  • Dynamic pricing based on distance
  • Different rates for vehicle types
  • Transparent fare display before booking

7. Protected Routes

  • User and Captain route protection
  • Automatic redirect if not authenticated
  • Session management with JWT

8. Responsive Design

  • Mobile-first UI with Tailwind CSS
  • Smooth animations with GSAP
  • Optimized for all screen sizes

๐Ÿ”ง CORS Configuration

The application allows requests from:

  • http://localhost:5173 (Local frontend)
  • https://lx36v5dk-5173.inc1.devtunnels.ms (Dev tunnel frontend)

To add more origins, update Backend/app.js:

const allowedOrigins = [
  'http://localhost:5173',
  'https://your-domain.com'
];

๐Ÿ› Troubleshooting

Backend Issues

Problem: "Cannot connect to MongoDB"

  • Ensure MongoDB is running (mongod)
  • Check DB_CONNECT in .env
  • Verify MongoDB URI format

Problem: "Socket connection refused"

  • Check if backend server is running on port 4000
  • Verify PORT in .env
  • Check CORS origins configuration

Problem: "Google Maps API error"

  • Verify GOOGLE_MAPS_API key in .env
  • Check API key has Maps & Geocoding enabled
  • Ensure billing is enabled on Google Cloud

Frontend Issues

Problem: "Blank page on localhost:5173"

  • Run npm install in Frontend directory
  • Check browser console for errors
  • Verify backend API is running

Problem: "Socket.IO connection failed"

  • Check backend is running
  • Verify socket URL in frontend code
  • Check CORS origins

Problem: "Can't find coordinates"

  • Verify Google Maps API key
  • Check address format
  • Ensure API has Geocoding enabled

๐Ÿ“ Example Workflow

Complete User Journey

1. User opens app (http://localhost:5173/)
   โ†“
2. Clicks "Signup" โ†’ Register as new user
   โ†“
3. Email & password saved in MongoDB
   โ†“
4. Redirected to login page
   โ†“
5. Login with credentials โ†’ JWT token generated
   โ†“
6. Token stored in cookies/localStorage
   โ†“
7. Redirected to /home (protected route)
   โ†“
8. User enters "Pickup: 123 Main St, Destination: 456 Oak Ave"
   โ†“
9. Frontend calls GET /rides/fare โ†’ Fare calculated ($15)
   โ†“
10. User selects vehicle type "Car"
    โ†“
11. User clicks "Book Ride" โ†’ POST /rides/create
    โ†“
12. Backend finds 3 captains within 5km
    โ†“
13. Socket.IO sends "new-ride" event to captains
    โ†“
14. User sees "Looking for Driver" spinner
    โ†“
15. Captain1 receives notification, views ride details
    โ†“
16. Captain1 accepts ride โ†’ POST /rides/confirm
    โ†“
17. User receives "ride-confirmed" event
    โ†“
18. Captain's location streamed to user in real-time
    โ†“
19. Captain arrives at pickup โ†’ Picks up user
    โ†“
20. Ride starts โ†’ Destination tracking active
    โ†“
21. Captain reaches destination โ†’ Ride completes
    โ†“
22. User rates captain (5 stars) & leaves feedback
    โ†“
23. Ride added to history for both user & captain
    โœ… Journey Complete!

๐Ÿš€ Deployment Tips

Backend Deployment (Heroku, Railway, Render)

# Ensure all env vars are set in deployment platform
# Ensure MongoDB Atlas connection is configured
# Push code to Git and deploy

Frontend Deployment (Vercel, Netlify)

# Update API base URL to production backend
# Build: npm run build
# Deploy dist folder

Environment Variables for Production

PORT=4000
DB_CONNECT=mongodb+srv://user:pass@cluster.mongodb.net/uber
JWT_SECRET=<strong-random-key>
GOOGLE_MAPS_API=<production-api-key>

๐Ÿ“š Additional Resources


๐Ÿ“„ License

ISC


๐Ÿ‘ค Author

Created as a learning project for full-stack web development


๐Ÿค Contributing

Feel free to fork and submit pull requests for improvements!


Last Updated: May 2026 Version: 1.0.0

About

online taxi app

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

โšก