A mini ride-sharing backend built with Spring Boot, MongoDB, and JWT Authentication.
- Spring Boot 3.2.0
- MongoDB (NoSQL Database)
- JWT (JSON Web Tokens for authentication)
- Spring Security
- Jakarta Bean Validation
- Maven
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)
- Java 17+
- Maven 3.6+
- MongoDB running on
localhost:27017
# Navigate to project directory
cd rideshare
# Build the project
mvn clean install
# Run the application
mvn spring-boot:runThe application will start on http://localhost:8081
POST /api/auth/register
Content-Type: application/json
{
"username": "john",
"password": "1234",
"role": "ROLE_USER"
}POST /api/auth/register
Content-Type: application/json
{
"username": "driver1",
"password": "abcd",
"role": "ROLE_DRIVER"
}POST /api/auth/login
Content-Type: application/json
{
"username": "john",
"password": "1234"
}
Response:
{
"token": "eyJhbGciOiJIUzI1NiJ9..."
}POST /api/v1/rides
Authorization: Bearer <token>
Content-Type: application/json
{
"pickupLocation": "Koramangala",
"dropLocation": "Indiranagar"
}GET /api/v1/user/rides
Authorization: Bearer <token>GET /api/v1/driver/rides/requests
Authorization: Bearer <token>POST /api/v1/driver/rides/{rideId}/accept
Authorization: Bearer <token>POST /api/v1/rides/{rideId}/complete
Authorization: Bearer <token>curl -X POST http://localhost:8081/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"john","password":"1234","role":"ROLE_USER"}'curl -X POST http://localhost:8081/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"driver1","password":"abcd","role":"ROLE_DRIVER"}'curl -X POST http://localhost:8081/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"john","password":"1234"}'Copy the token from response.
curl -X POST http://localhost:8081/api/v1/rides \
-H "Authorization: Bearer <USER_TOKEN>" \
-H "Content-Type: application/json" \
-d '{"pickupLocation":"Koramangala","dropLocation":"Indiranagar"}'β
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)
{
"_id": "ObjectId",
"username": "john",
"password": "$2a$10$...",
"role": "ROLE_USER"
}{
"_id": "ObjectId",
"userId": "user_id",
"driverId": "driver_id",
"pickupLocation": "Koramangala",
"dropLocation": "Indiranagar",
"status": "REQUESTED",
"createdAt": "2025-01-20T10:00:00Z"
}- Passwords are hashed using BCrypt
- JWT tokens expire after 24 hours
- Role-based access control for endpoints
- Stateless authentication (no sessions)
{
"error": "VALIDATION_ERROR",
"message": "Username is required",
"timestamp": "2025-01-20T12:00:00Z"
}Built with β and π