Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš• Ride Sharing Backend

A mini ride-sharing backend built with Spring Boot, MongoDB, and JWT Authentication.

πŸ—οΈ Tech Stack

  • Spring Boot 3.2.0
  • MongoDB (NoSQL Database)
  • JWT (JSON Web Tokens for authentication)
  • Spring Security
  • Jakarta Bean Validation
  • Maven

πŸ“ Project Structure

src/main/java/org/example/rideshare/
β”œβ”€β”€ model/              # Entity classes (User, Ride)
β”œβ”€β”€ repository/         # MongoDB repositories
β”œβ”€β”€ service/            # Business logic
β”œβ”€β”€ controller/         # REST endpoints
β”œβ”€β”€ config/             # Security & JWT configuration
β”œβ”€β”€ dto/                # Request/Response DTOs
β”œβ”€β”€ exception/          # Custom exceptions & global handler
└── util/               # Utility classes (JwtUtil)

πŸš€ Setup Instructions

Prerequisites

  • Java 17+
  • Maven 3.6+
  • MongoDB running on localhost:27017

Run the Application

# Navigate to project directory
cd rideshare

# Build the project
mvn clean install

# Run the application
mvn spring-boot:run

The application will start on http://localhost:8081

πŸ“‹ API Endpoints

πŸ”“ Public Endpoints (No Authentication)

Register User

POST /api/auth/register
Content-Type: application/json

{
  "username": "john",
  "password": "1234",
  "role": "ROLE_USER"
}

Register Driver

POST /api/auth/register
Content-Type: application/json

{
  "username": "driver1",
  "password": "abcd",
  "role": "ROLE_DRIVER"
}

Login

POST /api/auth/login
Content-Type: application/json

{
  "username": "john",
  "password": "1234"
}

Response:
{
  "token": "eyJhbGciOiJIUzI1NiJ9..."
}

πŸ”’ Protected Endpoints (Require JWT Token)

Create Ride (USER only)

POST /api/v1/rides
Authorization: Bearer <token>
Content-Type: application/json

{
  "pickupLocation": "Koramangala",
  "dropLocation": "Indiranagar"
}

View My Rides (USER only)

GET /api/v1/user/rides
Authorization: Bearer <token>

View Pending Ride Requests (DRIVER only)

GET /api/v1/driver/rides/requests
Authorization: Bearer <token>

Accept Ride (DRIVER only)

POST /api/v1/driver/rides/{rideId}/accept
Authorization: Bearer <token>

Complete Ride (USER or DRIVER)

POST /api/v1/rides/{rideId}/complete
Authorization: Bearer <token>

πŸ§ͺ Testing with CURL

1. Register a User

curl -X POST http://localhost:8081/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"john","password":"1234","role":"ROLE_USER"}'

2. Register a Driver

curl -X POST http://localhost:8081/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"driver1","password":"abcd","role":"ROLE_DRIVER"}'

3. Login as User

curl -X POST http://localhost:8081/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"john","password":"1234"}'

Copy the token from response.

4. Create a Ride

curl -X POST http://localhost:8081/api/v1/rides \
-H "Authorization: Bearer <USER_TOKEN>" \
-H "Content-Type: application/json" \
-d '{"pickupLocation":"Koramangala","dropLocation":"Indiranagar"}'

πŸ”‘ Features

βœ… User Registration with role-based access (USER/DRIVER)
βœ… JWT-based authentication and authorization
βœ… Password encryption with BCrypt
βœ… Input validation with Jakarta Bean Validation
βœ… Global exception handling
βœ… MongoDB integration
βœ… Clean architecture (Controller β†’ Service β†’ Repository)

πŸ“Š Database Collections

users

{
  "_id": "ObjectId",
  "username": "john",
  "password": "$2a$10$...",
  "role": "ROLE_USER"
}

rides

{
  "_id": "ObjectId",
  "userId": "user_id",
  "driverId": "driver_id",
  "pickupLocation": "Koramangala",
  "dropLocation": "Indiranagar",
  "status": "REQUESTED",
  "createdAt": "2025-01-20T10:00:00Z"
}

πŸ›‘οΈ Security

  • Passwords are hashed using BCrypt
  • JWT tokens expire after 24 hours
  • Role-based access control for endpoints
  • Stateless authentication (no sessions)

πŸ“ Error Response Format

{
  "error": "VALIDATION_ERROR",
  "message": "Username is required",
  "timestamp": "2025-01-20T12:00:00Z"
}

πŸ‘¨β€πŸ’» Author

Built with β˜• and πŸš€

Releases

Packages

Contributors

Languages