Skip to content

A robust RESTful API for blogging platform built with NestJS, TypeScript, and MySQL. Features JWT authentication, email verification, file uploads, social interactions, and Docker deployment. Implements clean architecture with generic email/upload modules using factory patterns.

License

Notifications You must be signed in to change notification settings

MohamedAboElnaser/Blog-apis

Repository files navigation

Blog APIs

A robust and scalable RESTful API for a blogging platform built with NestJS, TypeScript, and MySQL. This project demonstrates modern backend development practices with clean architecture, comprehensive authentication, and flexible service integrations.

Table of Contents


Features

  • User Authentication & Authorization - JWT-based auth with email verification and secure refresh tokens
  • Blog Management - Create, read, update, and delete blog posts (public/private)
  • Comment System - Users can comment on public blogs
  • Social Features - Follow/unfollow users and like blog posts
  • File Upload - Profile picture uploads with multiple storage providers
  • Email Integration - Verification emails and password reset functionality
  • Database Seeding - Pre-populated data for development and testing
  • API Documentation - Interactive Swagger/OpenAPI documentation
  • Dockerized - Ready for containerized deployment
  • Flexible Architecture - Generic email and upload modules using factory pattern

πŸ” Back to Top


Tech Stack

  • Framework: NestJS (Node.js)
  • Language: TypeScript
  • Database: MySQL with TypeORM
  • Authentication: JWT with refresh tokens
  • File Upload: Cloudinary (configurable)
  • Email: Multiple providers (Brevo, SendGrid, Mailtrap)
  • Documentation: Swagger/OpenAPI
  • Validation: class-validator & class-transformer
  • Containerization: Docker & Docker Compose

πŸ” Back to Top


Database Schema (ERD)

ERD

Architecture Highlights

This project showcases some interesting architectural patterns:

Generic Email Module

I've implemented a flexible email system using the Factory Pattern that allows switching between different email providers (Brevo, SendGrid, Mailtrap) based on the environment:

  • Development: Mailtrap (for testing)

  • Staging: SendGrid

  • Production: Brevo

    The beauty of this approach is that you can easily add new email providers without changing existing code - just implement the EmailProvider interface!

Configurable Upload Module

Similarly, the upload module is built to be provider-agnostic. Currently configured for Cloudinary, but designed to easily support other storage solutions like AWS S3, Firebase, or local storage through the same factory pattern approach. πŸ” Back to Top


Getting Started

You have two options to run this project: Docker (recommended) or Local Development.

Option 1: Docker Setup (Recommended) 🐳

This is the easiest way to get up and running quickly!

Prerequisites

  • Docker
  • Docker Compose

Quick Start

  1. Clone the repository
git clone https://github.com/MohamedAboElnaser/Blog-apis.git
cd Blog-apis
  1. Environment Setup
# Copy the example environment file
cp .env.example .env

# Edit the .env file with your configurations
nano .env  # or use your preferred editor
  1. Run with Docker Compose
# For development (with hot reload)
docker compose up --build

# For production
docker compose -f docker-compose.prod.yml up --build -d
  1. Seed the database (optional)
# Access the running container
docker compose exec app npm run seed

# Or force seed (clears existing data)
docker compose exec app npm run seed:force

That's it! Your API will be available at http://localhost:3000

Option 2: Local Development Setup πŸ’»

Prerequisites

  • Node.js (>=18)
  • MySQL database
  • npm or yarn

Installation Steps

  1. Clone the repository
git clone https://github.com/MohamedAboElnaser/Blog-apis.git
cd Blog-apis
  1. Install dependencies
npm install
  1. Environment Setup
# Copy the example environment file
cp .env.example .env

# Edit the .env file with your configurations
nano .env  # or use your preferred editor
  1. Database Setup

    Make sure your MySQL database is running and create the database specified in your .env file.

  2. Run the application

# Development mode
npm run start:dev

# Production mode
npm run build
npm run start:prod
  1. Seed the database (optional)
# Regular seeding (skips if data exists)
npm run seed

# Force seeding (clears existing data)
npm run seed:force

πŸ” Back to Top


Environment Configuration

Create a .env file based on .env.example and fill in your specific configurations. This file contains sensitive information like database credentials, JWT secrets, and email provider settings. πŸ” Back to Top


API Documentation

You can access the API documentation in two ways:

Interactive Documentation (Local)

Visit http://localhost:3000/api to access the interactive Swagger documentation when running the application locally. Here you'll find detailed information about all endpoints, request/response schemas, and can even test the API directly from the browser!

Static Documentation (GitHub Pages)

πŸ“š View Static API Documentation - Complete API documentation that's always available, automatically updated with each release. No need to run the application locally!

Note: The GitHub Pages documentation is static and doesn't allow testing endpoints directly. For interactive testing, use the local documentation after running the application.

πŸ” Back to Top


Quick API Overview

Authentication Endpoints

  • POST /auth/register - Register a new user
  • POST /auth/verify-email - Verify email with OTP
  • POST /auth/login - User login (returns access token + sets refresh token cookie)
  • POST /auth/refresh-token - Refresh expired access token using cookie
  • POST /auth/request-password-reset - Request password reset
  • POST /auth/reset-password - Reset password with OTP
  • POST /auth/resend-verification-code - Resend email verification code

Blog Endpoints

  • GET /blogs - Get user's blogs (authenticated)
  • POST /blogs - Create a new blog
  • GET /blogs/ - Get all public blogs
  • GET /blogs/:id - Get specific blog details
  • PATCH /blogs/:id - Update blog
  • DELETE /blogs/:id - Delete blog

User & Social Features

  • GET /users/me - Get current user profile
  • GET /users/:id/blogs - Get user's public blogs
  • POST /users/:id/follow - Follow a user
  • DELETE /users/:id/unfollow - Unfollow a user
  • POST /blogs/:id/like - Like a blog
  • DELETE /blogs/:id/unlike - Unlike a blog

πŸ” Back to Top


Authentication & Security

JWT Token Management

The API uses a dual-token authentication system for enhanced security:

  • Access Token: Short-lived JWT token (15-30 minutes) sent in response body

    • Used for authenticating API requests
    • Include in Authorization header: Bearer <access_token>
    • Expires quickly for security
  • Refresh Token: Long-lived token (7 days default) stored in HTTP-only cookie

    • Used to obtain new access tokens when they expire
    • Automatically sent with requests (HTTP-only cookie)
    • Cannot be accessed by JavaScript (XSS protection)

Security Features

  • HTTP-Only Cookies: Refresh tokens stored securely, inaccessible to client-side scripts
  • Environment-Based Security: HTTPS-only cookies in production
  • Configurable Expiration: Token lifetimes configurable via environment variables
  • Automatic Token Refresh: Seamless token renewal without user intervention

Usage Example:

  1. Login β†’ Receive access token + refresh token cookie
  2. Use access token for API calls
  3. When access token expires β†’ Call /auth/refresh-token
  4. Receive new access token (refresh token cookie updated automatically)

πŸ” Back to Top


Project Structure

src/
β”œβ”€β”€ auth/              # Authentication module (JWT, guards, strategies)
β”œβ”€β”€ blog/              # Blog management (CRUD operations)
β”œβ”€β”€ comment/           # Comment system for blogs
β”œβ”€β”€ common/            # Shared utilities and DTOs
β”œβ”€β”€ database/          # Database configuration and seeders
β”œβ”€β”€ email/             # Generic email module with multiple providers
β”œβ”€β”€ follow/            # User following system
β”œβ”€β”€ like/              # Blog liking functionality
β”œβ”€β”€ otp/               # OTP generation and verification
β”œβ”€β”€ upload/            # Generic file upload module
β”œβ”€β”€ user/              # User management and profiles
β”œβ”€β”€ app.module.ts      # Main application module
└── main.ts           # Application entry point

scripts/
└── seed.ts           # Database seeding script

docker-compose.yml          # Development Docker setup
docker-compose.prod.yml     # Production Docker setup
Dockerfile                  # Multi-stage Docker build
.env.example               # Environment variables template

πŸ” Back to Top


Database Seeding

The project includes a comprehensive seeding system that creates:

  • Test users with verified accounts
  • Sample blog posts (public and private)
  • Comments on public blogs
  • Follow relationships between users
  • Likes on blog posts

This makes it super easy to get started with development - just run the seed command and you'll have a fully populated database to work with!

Seeded User Credentials

⚠️ Important: To use the seeded data, make sure your .env file has the following SALT value:

SALT='$2b$10$CfyyfGZKr/OjMf6wJ34Na.'

All seeded users use the password: pass1234

Available Test Users:

  • admin@blog.com (Admin User)
  • john.doe@example.com (John Doe)
  • jane.smith@example.com (Jane Smith)
  • mike.wilson@example.com (Mike Wilson)
  • sarah.jones@example.com (Sarah Jones)

You can login with any of these emails using the password pass1234 to test the application functionality immediately!

πŸ” Back to Top


Contributing

I'd love your contributions! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes and add tests if applicable
  4. Commit your changes (git commit -m 'Add some amazing feature')
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request πŸ” Back to Top

Development Guidelines

  • Follow the existing code style and patterns
  • Update documentation if needed
  • Use descriptive commit messages
  • Test both Docker and local setups πŸ” Back to Top

Environment-Specific Features

The application automatically adapts based on the NODE_ENV:

  • Development: Uses Mailtrap for emails, detailed logging
  • Staging: Uses SendGrid for emails
  • Production: Uses Brevo for emails, optimized logging, HTTPS-only cookies

Deployment

The project is production-ready with:

  • Multi-stage Docker builds for optimized images
  • Production Docker Compose configuration
  • Environment-based configurations
  • Health checks and restart policies
  • Secure token management with HTTP-only cookies

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you have any questions or run into issues, feel free to:

  • Open an issue on GitHub
  • Check the Swagger documentation at /api
  • Review the seeded data examples πŸ” Back to Top

About

A robust RESTful API for blogging platform built with NestJS, TypeScript, and MySQL. Features JWT authentication, email verification, file uploads, social interactions, and Docker deployment. Implements clean architecture with generic email/upload modules using factory patterns.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages