Skip to content

Next.js backend API for a social media platform with TypeScript, MongoDB, Cloudinary integration, and email verification using Resend.

License

Notifications You must be signed in to change notification settings

PriteshThorat/SociyaBackend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Sociya Backend

A robust and scalable social media platform backend built with Next.js 16, TypeScript, and MongoDB. This API powers a Twitter-like social media application with features including user authentication, tweet management, likes, image uploads, and real-time email verification.

πŸš€ Features

Authentication & Security

  • JWT-based Authentication with access and refresh tokens
  • Email Verification using OTP (One-Time Password)
  • Rate Limiting with Upstash Redis (20 requests/minute)
  • Password Hashing with bcrypt
  • HTTP-only Cookies for secure token storage
  • CORS Protection with configurable origins

User Management

  • User registration with email verification
  • Secure login/logout
  • Profile management with avatar uploads
  • Username uniqueness validation
  • Password change functionality
  • Token refresh mechanism

Tweet Features

  • Create tweets with optional image attachments
  • Update existing tweets
  • Delete tweets
  • View all tweets (home feed)
  • View user-specific tweets
  • Like/Unlike tweets
  • Tweet aggregation with like counts

Media Handling

  • Image Upload to Cloudinary
  • Image Optimization with URL transformations
  • Automatic Image Deletion when tweets are removed
  • Support for formdata and multipart uploads

πŸ› οΈ Tech Stack

  • Framework: Next.js 16 (App Router)
  • Language: TypeScript
  • Database: MongoDB with Mongoose ODM
  • Authentication: JWT (jsonwebtoken)
  • File Upload: Formidable
  • Image Storage: Cloudinary
  • Email Service: Resend
  • Rate Limiting: Upstash Redis + Vercel KV
  • Password Hashing: bcrypt
  • Email Templates: React Email

πŸ“ Project Structure

sociyabackend/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   └── api/
β”‚   β”‚       └── v1/
β”‚   β”‚           β”œβ”€β”€ users/          # User endpoints
β”‚   β”‚           β”‚   β”œβ”€β”€ create-account/
β”‚   β”‚           β”‚   β”œβ”€β”€ login/
β”‚   β”‚           β”‚   β”œβ”€β”€ logout/
β”‚   β”‚           β”‚   β”œβ”€β”€ verify-otp/
β”‚   β”‚           β”‚   β”œβ”€β”€ me/
β”‚   β”‚           β”‚   β”œβ”€β”€ is-username-unique/
β”‚   β”‚           β”‚   β”œβ”€β”€ new-access-token/
β”‚   β”‚           β”‚   β”œβ”€β”€ c/password/
β”‚   β”‚           β”‚   β”œβ”€β”€ r/otp/
β”‚   β”‚           β”‚   └── u/avatar/
β”‚   β”‚           β”œβ”€β”€ tweet/          # Tweet endpoints
β”‚   β”‚           β”‚   └── t/
β”‚   β”‚           β”‚       β”œβ”€β”€ upload/
β”‚   β”‚           β”‚       β”œβ”€β”€ update/[tweetId]/
β”‚   β”‚           β”‚       └── delete/[tweetId]/
β”‚   β”‚           β”œβ”€β”€ like/           # Like endpoints
β”‚   β”‚           β”‚   └── tweet/[tweetId]/
β”‚   β”‚           β”œβ”€β”€ home/           # Feed endpoints
β”‚   β”‚           β”‚   β”œβ”€β”€ all-content/
β”‚   β”‚           β”‚   └── user-content/[username]/
β”‚   β”‚           └── healthcheck/    # Health check
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ user.model.ts          # User schema
β”‚   β”‚   β”œβ”€β”€ tweet.model.ts         # Tweet schema
β”‚   β”‚   └── like.model.ts          # Like schema
β”‚   β”œβ”€β”€ middlewares/
β”‚   β”‚   β”œβ”€β”€ auth.middleware.ts     # JWT verification
β”‚   β”‚   └── parseImageUpload.middleware.ts
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ ApiResponse.ts         # Standardized API responses
β”‚   β”‚   β”œβ”€β”€ cloudinary.ts          # Cloudinary integration
β”‚   β”‚   β”œβ”€β”€ dbConnect.ts           # MongoDB connection
β”‚   β”‚   β”œβ”€β”€ generateAccessAndRefreshToken.ts
β”‚   β”‚   β”œβ”€β”€ sendVerificationEmail.ts
β”‚   β”‚   β”œβ”€β”€ deleteFromCloudinary.ts
β”‚   β”‚   β”œβ”€β”€ getOptimizedUrl.ts     # Image optimization
β”‚   β”‚   └── resend.ts              # Email service
β”‚   β”œβ”€β”€ emails/
β”‚   β”‚   └── verificationEmail.tsx  # Email template
β”‚   └── middleware.ts              # Rate limiting & CORS
β”œβ”€β”€ public/
β”‚   └── temp/                      # Temporary file storage
β”œβ”€β”€ next.config.ts
β”œβ”€β”€ tsconfig.json
└── package.json

API Endpoints

Health Check

  • GET /api/v1/healthcheck - Check API status

Authentication

  • POST /api/v1/users/create-account - Register new user
  • POST /api/v1/users/login - User login
  • POST /api/v1/users/logout - User logout (requires auth)
  • POST /api/v1/users/verify-otp - Verify email with OTP
  • POST /api/v1/users/r/otp - Resend OTP
  • POST /api/v1/users/new-access-token - Refresh access token

User

  • GET /api/v1/users/me - Get current user profile (requires auth)
  • GET /api/v1/users/is-username-unique - Check username availability
  • PATCH /api/v1/users/c/password - Change password (requires auth)
  • PATCH /api/v1/users/u/avatar - Update avatar (requires auth)

Tweets

  • POST /api/v1/tweet/t/upload - Create new tweet (requires auth)
  • PATCH /api/v1/tweet/t/update/[tweetId] - Update tweet (requires auth)
  • DELETE /api/v1/tweet/t/delete/[tweetId] - Delete tweet (requires auth)

Feed

  • GET /api/v1/home/all-content - Get all tweets
  • GET /api/v1/home/user-content/[username] - Get user tweets

Likes

  • POST /api/v1/like/tweet/[tweetId] - Toggle like on tweet (requires auth)

πŸ” Authentication Flow

  1. Registration:

    • User submits credentials
    • Password is hashed
    • OTP is generated and sent via email
    • User account is created (unverified)
  2. Email Verification:

    • User enters OTP
    • OTP is validated (5-minute expiry)
    • Account is marked as verified
  3. Login:

    • User submits credentials
    • Password is verified
    • Access token (1d) and refresh token (10d) are generated
    • Tokens are set as HTTP-only cookies
  4. Protected Routes:

    • Middleware verifies JWT from cookie or Authorization header
    • User information is attached to request

πŸ“¦ Database Schema

User Model

{
  username: string(unique);
  fullName: string;
  email: string(unique);
  avatar: string;
  avatarId: string;
  password: string(hashed);
  refreshToken: string;
  isVerified: boolean;
  otp: string;
  otpExpiresAt: Date;
  timestamps: true;
}

Tweet Model

{
  content: string
  image: string
  imageId: string
  owner: ObjectId (ref: User)
  timestamps: true
}

Like Model

{
  likedBy: ObjectId (ref: User)
  likedTo: ObjectId (ref: Tweet)
  timestamps: true
}

🌐 CORS Configuration

The API is configured to accept requests from a specific origin (configurable via CORS_ORIGIN env variable). All API routes support:

  • Credentials
  • Custom headers
  • Standard HTTP methods (GET, POST, PATCH, DELETE, OPTIONS)

🚦 Rate Limiting

Built-in rate limiting using Upstash Redis:

  • 20 requests per minute per IP address
  • Sliding window algorithm
  • Returns 429 status when limit is exceeded

πŸ“§ Email Verification

Uses Resend service with custom React email templates:

  • OTP generation with 6-digit code
  • 5-minute expiration
  • Resend OTP functionality
  • Professional email templates

πŸ–ΌοΈ Image Handling

Cloudinary integration for efficient image management:

  • Automatic upload to sociya-v1 folder
  • Image optimization with URL transformations
  • Automatic deletion when content is removed
  • Support for avatar and tweet images

πŸ”’ Security Features

  • HTTP-only cookies for token storage
  • CORS protection
  • Rate limiting
  • JWT-based authentication
  • Password hashing with bcrypt
  • Input validation
  • MongoDB injection protection via Mongoose

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

This project is private and proprietary.

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

Built with ❀️ for the Sociya social media platform


Note: This is a backend API service. Make sure to configure your frontend application to point to the correct API endpoints.

About

Next.js backend API for a social media platform with TypeScript, MongoDB, Cloudinary integration, and email verification using Resend.

Topics

Resources

License

Stars

Watchers

Forks