Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PrimeTrade - Scalable REST API with Authentication & Role-Based Access

A full-stack application featuring a secure, scalable backend API with JWT authentication, role-based access control, and a React frontend for task management.

πŸš€ Features

Backend

  • βœ… User registration & login with JWT authentication
  • βœ… Password hashing using bcrypt
  • βœ… Role-based access control (User & Admin)
  • βœ… CRUD operations for Tasks entity
  • βœ… API versioning (v1)
  • βœ… Comprehensive error handling & validation
  • βœ… Swagger API documentation
  • βœ… MongoDB database with Mongoose ODM
  • βœ… Security features (Helmet, CORS, Rate Limiting)
  • βœ… Request logging with Morgan

Frontend

  • βœ… React.js with Vite
  • βœ… User authentication (Register/Login)
  • βœ… Protected routes with JWT
  • βœ… Task management dashboard
  • βœ… CRUD operations for tasks
  • βœ… Responsive UI design
  • βœ… Real-time error/success messages

πŸ“‹ Prerequisites

  • Node.js (v16 or higher)
  • MongoDB Atlas account (or local MongoDB)
  • npm or yarn package manager

πŸ› οΈ Installation & Setup

1. Clone the Repository

git clone <your-repo-url>
cd primetrade

2. Backend Setup

cd backend
npm install

Create a .env file in the backend directory:

PORT=5000
MONGODB_URI=mongodb+srv://harshtanishq2002_db_user:FcHAClWNONYHvetB@cluster0.w86isu1.mongodb.net/primetrade?retryWrites=true&w=majority
JWT_SECRET=your_super_secret_jwt_key_change_this_in_production_12345
JWT_EXPIRE=7d
NODE_ENV=development

Start the backend server:

npm run dev

The server will start at http://localhost:5000 Swagger documentation will be available at http://localhost:5000/api-docs

3. Frontend Setup

cd frontend
npm install

Create a .env file in the frontend directory:

VITE_API_URL=http://localhost:5000/api/v1

Start the frontend development server:

npm run dev

The frontend will start at http://localhost:5173

πŸ“š API Documentation

Once the backend is running, access the Swagger documentation at:

http://localhost:5000/api-docs

API Endpoints

Authentication Routes (/api/v1/auth)

  • POST /register - Register a new user
  • POST /login - Login user
  • GET /me - Get current user (Protected)
  • PUT /profile - Update user profile (Protected)

Task Routes (/api/v1/tasks)

  • GET /tasks - Get all tasks (Protected)
  • GET /tasks/:id - Get single task (Protected)
  • POST /tasks - Create new task (Protected)
  • PUT /tasks/:id - Update task (Protected)
  • DELETE /tasks/:id - Delete task (Protected)
  • GET /tasks/stats - Get task statistics (Admin only)

πŸ—„οΈ Database Schema

User Model

{
  name: String,
  email: String (unique),
  password: String (hashed),
  role: String (enum: ['user', 'admin']),
  isActive: Boolean,
  createdAt: Date,
  updatedAt: Date
}

Task Model

{
  title: String,
  description: String,
  status: String (enum: ['pending', 'in-progress', 'completed']),
  priority: String (enum: ['low', 'medium', 'high']),
  dueDate: Date,
  user: ObjectId (ref: User),
  createdAt: Date,
  updatedAt: Date
}

πŸ”’ Security Features

  1. JWT Authentication - Secure token-based authentication
  2. Password Hashing - bcrypt with salt rounds
  3. Input Validation - express-validator for request validation
  4. Rate Limiting - Prevent brute force attacks (100 requests per 10 minutes)
  5. Helmet - Security headers
  6. CORS - Cross-Origin Resource Sharing configuration
  7. Role-Based Access - User and Admin roles with different permissions

πŸ“ Project Structure

primetrade/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ database.js
β”‚   β”‚   └── swagger.js
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ authController.js
β”‚   β”‚   └── taskController.js
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ auth.js
β”‚   β”‚   β”œβ”€β”€ errorHandler.js
β”‚   β”‚   └── validator.js
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ User.js
β”‚   β”‚   └── Task.js
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ authRoutes.js
β”‚   β”‚   └── taskRoutes.js
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ generateToken.js
β”‚   β”‚   └── responseHandler.js
β”‚   β”œβ”€β”€ .env
β”‚   β”œβ”€β”€ .gitignore
β”‚   β”œβ”€β”€ package.json
β”‚   └── server.js
β”‚
└── frontend/
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ components/
    β”‚   β”‚   β”œβ”€β”€ Login.jsx
    β”‚   β”‚   β”œβ”€β”€ Register.jsx
    β”‚   β”‚   β”œβ”€β”€ Dashboard.jsx
    β”‚   β”‚   β”œβ”€β”€ PrivateRoute.jsx
    β”‚   β”‚   β”œβ”€β”€ Auth.css
    β”‚   β”‚   └── Dashboard.css
    β”‚   β”œβ”€β”€ services/
    β”‚   β”‚   β”œβ”€β”€ authService.js
    β”‚   β”‚   └── taskService.js
    β”‚   β”œβ”€β”€ utils/
    β”‚   β”‚   └── api.js
    β”‚   β”œβ”€β”€ App.jsx
    β”‚   β”œβ”€β”€ App.css
    β”‚   β”œβ”€β”€ main.jsx
    β”‚   └── index.css
    β”œβ”€β”€ .env
    β”œβ”€β”€ package.json
    └── vite.config.js

πŸ§ͺ Testing the Application

Using the Frontend

  1. Register a new user at http://localhost:5173/register
  2. Login with credentials
  3. Create, view, update, and delete tasks
  4. Filter tasks by status
  5. Logout when done

Using Swagger UI

  1. Go to http://localhost:5000/api-docs
  2. Test each endpoint
  3. Use the "Authorize" button to add JWT token

Using Postman

Import the API endpoints and test each route with proper authentication headers.

πŸš€ Scalability Considerations

Current Implementation

  • Modular Architecture - Separated concerns (routes, controllers, models, middleware)
  • API Versioning - Prepared for future API changes without breaking existing clients
  • Database Indexing - Indexes on frequently queried fields for better performance
  • Error Handling - Centralized error handling for consistency
  • Input Validation - Prevents invalid data from reaching the database

Future Scalability Enhancements

1. Microservices Architecture

  • Separate authentication service
  • Dedicated task management service
  • User management service
  • API Gateway for routing

2. Caching Layer

  • Redis for session management
  • Cache frequently accessed data
  • Reduce database load
// Example Redis implementation
import redis from "redis";
const client = redis.createClient();

// Cache user data
await client.set(`user:${userId}`, JSON.stringify(userData), "EX", 3600);

3. Database Optimization

  • Sharding - Distribute data across multiple databases
  • Read Replicas - Separate read and write operations
  • Connection Pooling - Reuse database connections

4. Load Balancing

  • Use NGINX or AWS ELB
  • Distribute traffic across multiple server instances
  • Auto-scaling based on demand

5. Message Queues

  • RabbitMQ or Apache Kafka for async processing
  • Background jobs for email notifications
  • Task processing queues

6. CDN Integration

  • CloudFlare or AWS CloudFront for static assets
  • Reduce latency for global users

7. Monitoring & Logging

  • ELK Stack (Elasticsearch, Logstash, Kibana)
  • Prometheus + Grafana for metrics
  • Sentry for error tracking

8. Containerization & Orchestration

  • Docker for containerization
  • Kubernetes for orchestration
  • CI/CD pipeline with GitHub Actions

🐳 Docker Deployment (Optional)

Backend Dockerfile

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 5000
CMD ["node", "server.js"]

Frontend Dockerfile

FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

πŸ“Š Performance Optimization

  1. Database Queries

    • Use projection to fetch only required fields
    • Implement pagination for large datasets
    • Use aggregation pipelines efficiently
  2. API Response

    • Compress responses with gzip
    • Implement response caching
    • Use ETags for conditional requests
  3. Code Optimization

    • Use async/await properly
    • Avoid blocking operations
    • Implement connection pooling

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

πŸ“ License

This project is licensed under the ISC License.

πŸ‘€ Author

Harsh Tanishq

πŸ™ Acknowledgments

  • Express.js for the backend framework
  • React.js for the frontend
  • MongoDB for the database
  • JWT for authentication
  • Swagger for API documentation

Note: This is an intern assignment project demonstrating scalable backend development with authentication, role-based access, and a functional frontend UI.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages