Skip to content

PriteshThorat/Social-Media-Platform-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

37 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Social Media Platform Backend

A robust and scalable RESTful API backend for a social media platform built with Node.js, Express, and MongoDB. This backend supports user authentication, tweet management, likes, and real-time content delivery with advanced features like OTP verification, image uploads, and comprehensive user management.

✨ Features

πŸ” Authentication & Security

  • JWT-based Authentication with access and refresh tokens
  • OTP Email Verification for new accounts
  • Password Management with bcrypt encryption
  • Rate Limiting to prevent abuse (100 requests per 15 minutes)
  • Cookie-based Session Management
  • CORS Configuration for secure cross-origin requests

πŸ‘€ User Management

  • User registration with email verification
  • Login/Logout functionality
  • Profile management with avatar uploads
  • Password change capability
  • Token refresh mechanism
  • Get current user details

πŸ“ Tweet Features

  • Create tweets with text and optional images
  • Update existing tweets
  • Delete tweets
  • View all tweets (public feed)
  • View user-specific tweets
  • Image optimization via Cloudinary

❀️ Engagement

  • Like/Unlike tweets
  • Track likes per tweet
  • User engagement analytics

πŸ“Š Dashboard & Analytics

  • Get all tweets with user information
  • Get user-specific content
  • Optional authentication for public content viewing

πŸ› οΈ Tech Stack

  • Runtime: Node.js
  • Framework: Express.js 5.x
  • Database: MongoDB with Mongoose ODM
  • Authentication: JWT (JSON Web Tokens)
  • Image Storage: Cloudinary
  • Email Service: Resend / Nodemailer / EmailJS
  • File Upload: Multer
  • Security: bcrypt, express-rate-limit, CORS, compression

πŸ“ Project Structure

src/
β”œβ”€β”€ app.js                      # Express app configuration
β”œβ”€β”€ index.js                    # Entry point
β”œβ”€β”€ constants.js                # Application constants
β”œβ”€β”€ controllers/                # Request handlers
β”‚   β”œβ”€β”€ user.controller.js      # User operations
β”‚   β”œβ”€β”€ tweet.controller.js     # Tweet CRUD operations
β”‚   β”œβ”€β”€ like.controller.js      # Like/Unlike operations
β”‚   β”œβ”€β”€ dashboard.controller.js # Feed & analytics
β”‚   └── healthcheck.controller.js
β”œβ”€β”€ models/                     # Database schemas
β”‚   β”œβ”€β”€ user.model.js          # User schema with auth methods
β”‚   β”œβ”€β”€ tweet.model.js         # Tweet schema
β”‚   β”œβ”€β”€ like.model.js          # Like relationships
β”‚   └── follower.model.js      # Follower relationships
β”œβ”€β”€ routes/                    # API routes
β”‚   β”œβ”€β”€ user.routes.js
β”‚   β”œβ”€β”€ tweet.routes.js
β”‚   β”œβ”€β”€ like.routes.js
β”‚   β”œβ”€β”€ dashboard.routes.js
β”‚   └── healthcheck.routes.js
β”œβ”€β”€ middlewares/               # Custom middleware
β”‚   β”œβ”€β”€ auth.middleware.js     # JWT verification
β”‚   β”œβ”€β”€ optionalAuth.middleware.js
β”‚   β”œβ”€β”€ multer.middleware.js   # File upload handling
β”‚   └── errorHandler.middleware.js
β”œβ”€β”€ utils/                     # Helper functions
β”‚   β”œβ”€β”€ ApiResponse.js         # Standardized responses
β”‚   β”œβ”€β”€ ApiError.js           # Custom error class
β”‚   β”œβ”€β”€ asyncHandler.js       # Async error wrapper
β”‚   β”œβ”€β”€ cloudinary.js         # Image upload service
β”‚   β”œβ”€β”€ deleteFromCloudinary.js
β”‚   β”œβ”€β”€ getOptimizedUrl.js
β”‚   └── sendOTP.js            # Email OTP service
└── db/
    └── index.js              # Database connection

API Endpoints

πŸ” Authentication & User Routes

Base URL: /api/v1/users

Method Endpoint Description Auth Required
POST /create-account Register a new user ❌
POST /login User login ❌
POST /verify-otp Verify email with OTP ❌
POST /r/otp Request new OTP ❌
GET /logout User logout βœ…
GET /me Get current user details βœ…
GET /new-access-token Refresh access token ❌
PATCH /u/avatar Update user avatar βœ…
PATCH /c/password Change password ❌

πŸ“ Tweet Routes

Base URL: /api/v1/tweet

Method Endpoint Description Auth Required
POST /t/upload Create a new tweet βœ…
PATCH /t/update/:tweetId Update tweet βœ…
DELETE /t/delete/:tweetId Delete tweet βœ…

❀️ Like Routes

Base URL: /api/v1/like

Method Endpoint Description Auth Required
GET /tweet/:tweetId Toggle like on tweet βœ…

πŸ“Š Dashboard Routes

Base URL: /api/v1/home

Method Endpoint Description Auth Required
GET /all-content Get all tweets (feed) Optional
GET /user-content/:username Get user's tweets Optional

πŸ₯ Health Check

Base URL: /api/v1/healthcheck

Method Endpoint Description Auth Required
GET / Server health status ❌

πŸ”’ Authentication Flow

  1. Register: User creates account β†’ OTP sent to email
  2. Verify: User enters OTP β†’ Account activated
  3. Login: User logs in β†’ Receives access & refresh tokens
  4. Protected Routes: Access token required in cookies
  5. Token Refresh: Refresh token generates new access token
  6. Logout: Clears tokens from cookies

πŸ“ Request/Response Examples

Create Account

POST /api/v1/users/create-account
Content-Type: application/json

{
  "username": "johndoe",
  "fullName": "John Doe",
  "email": "john@example.com",
  "password": "SecurePass123"
}

Upload Tweet

POST /api/v1/tweet/t/upload
Content-Type: multipart/form-data
Authorization: Bearer <token>

{
  "content": "Hello World! This is my first tweet",
  "image": <file>
}

Get All Tweets

GET /api/v1/home/all-content

πŸ›‘οΈ Security Features

  • Password Hashing: bcrypt with 10 salt rounds
  • JWT Tokens: Separate access (1d) and refresh tokens (10d)
  • Rate Limiting: 100 requests per 15 minutes per IP
  • HTTP-only Cookies: Secure token storage
  • Data Validation: Required field validation
  • Error Handling: Centralized error middleware
  • CORS Protection: Configurable origin whitelist
  • File Upload Limits: 16kb JSON payload limit

Dependencies

Core Dependencies

  • express - Web framework
  • mongoose - MongoDB ODM
  • jsonwebtoken - JWT authentication
  • bcrypt - Password hashing
  • multer - File upload handling
  • cloudinary - Image storage
  • cookie-parser - Cookie parsing
  • cors - CORS middleware
  • express-rate-limit - Rate limiting
  • compression - Response compression
  • resend - Email service

Dev Dependencies

  • nodemon - Development auto-reload
  • prettier - Code formatting

πŸ“„ License

Copyright (c) 2025 Pritesh Thorat - All Rights Reserved. This code is available for viewing and educational purposes only. See the LICENSE file for details.

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

Pritesh Thorat

πŸ› Bug Reports & Issues

Please report bugs and issues on the GitHub Issues page.


Made with ❀️ by Pritesh Thorat

About

RESTful API backend for a social media platform built with Node.js, Express, and MongoDB. Features authentication, tweet management, likes, followers, and media uploads with Cloudinary.

Topics

Resources

License

Stars

Watchers

Forks

Contributors