Skip to content

sheraz61/BlogApplicaiton-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 

Repository files navigation

BlogApi - Full Stack Blog Application

A modern blog application built with Node.js, Express, MongoDB, and React. This project features a comprehensive backend API with user authentication, blog post management, social features, and advanced admin functionality.

πŸš€ Features

User Authentication & Management

  • User Registration - Secure user registration with username/password
  • User Login/Logout - JWT-based authentication with secure cookie handling
  • Profile Management - Edit user profiles and manage personal information
  • Account Deletion - Secure account deletion functionality
  • Login History - Track user login activities
  • Public Profiles - View other users' public profiles

Profile Image Management

  • Upload Profile Image - Upload and update profile pictures using Cloudinary
  • Delete Profile Image - Remove profile images with cloud storage cleanup
  • Image Storage - Secure cloud-based image storage with public_id tracking

Email Verification System

  • Email Addition - Add email addresses to user accounts
  • Email Verification - Secure email verification with OTP codes
  • Email Change - Change email addresses with verification process
  • Verification Cancellation - Cancel pending email verification processes

Password Management

  • Password Change - Secure password change for authenticated users
  • Forgot Password - Password reset functionality with email verification
  • Password Reset Verification - OTP-based password reset verification
  • Reset Cancellation - Cancel pending password reset processes

Blog Post Management

  • Create Posts - Create blog posts with text content and images
  • Update Posts - Edit existing blog posts
  • Delete Posts - Remove blog posts with proper cleanup
  • View Posts - Get all posts or specific post by ID
  • Image Upload - Upload images for blog posts using Cloudinary

Social Features

  • Like System - Like and unlike blog posts
  • Comment System - Add, edit, and delete comments on posts
  • Bookmark System - Save posts to personal bookmarks
  • View Comments - Get all comments for a specific post

Admin Features

  • Admin Dashboard - Overview of users, verified users, and posts
  • User Management - View all users, verified users, and user details
  • Post Management - View all posts and manage content
  • User Deletion - Delete users by admin
  • Post Deletion - Delete any post by admin
  • Admin Authentication - Role-based access control for admin functions

Security Features

  • JWT Authentication - Secure token-based authentication
  • Password Hashing - Bcrypt password encryption
  • CORS Configuration - Cross-origin resource sharing setup
  • Input Validation - Request validation and sanitization
  • Rate Limiting - Protection against brute force attacks
  • Role-based Access - Admin and user role management

πŸ› οΈ Tech Stack

Backend

  • Node.js - JavaScript runtime environment
  • Express.js - Web application framework
  • MongoDB - NoSQL database
  • Mongoose - MongoDB object modeling
  • JWT - JSON Web Token authentication
  • Bcrypt - Password hashing
  • Multer - File upload handling
  • Cloudinary - Cloud image storage
  • Nodemailer - Email sending functionality
  • CORS - Cross-origin resource sharing
  • Cookie Parser - Cookie handling

Development Tools

  • Nodemon - Development server with auto-restart
  • Dotenv - Environment variable management

πŸ“ Project Structure

BlogApi/
β”œβ”€β”€ Backend/
β”‚   β”œβ”€β”€ app.js                 # Express app configuration
β”‚   β”œβ”€β”€ index.js               # Server entry point
β”‚   β”œβ”€β”€ package.json           # Backend dependencies
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── cloudinary.js      # Cloudinary configuration
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ user.controller.js # User-related operations
β”‚   β”‚   β”œβ”€β”€ admin.controller.js # Admin operations
β”‚   β”‚   β”œβ”€β”€ post.controller.js  # Blog post operations
β”‚   β”‚   β”œβ”€β”€ comment.controller.js # Comment operations
β”‚   β”‚   β”œβ”€β”€ like.controller.js  # Like operations
β”‚   β”‚   └── bookmark.controller.js # Bookmark operations
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   └── index.js           # Database connection
β”‚   β”œβ”€β”€ middlewares/
β”‚   β”‚   β”œβ”€β”€ auth.js            # JWT authentication middleware
β”‚   β”‚   β”œβ”€β”€ isAdmin.js         # Admin role verification
β”‚   β”‚   β”œβ”€β”€ multer.js          # File upload middleware
β”‚   β”‚   └── setUploadTraget.js # Upload target configuration
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ user.model.js      # User data model
β”‚   β”‚   β”œβ”€β”€ post.model.js      # Post data model
β”‚   β”‚   └── comment.model.js   # Comment data model
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ user.routes.js     # User API endpoints
β”‚   β”‚   β”œβ”€β”€ admin.routes.js    # Admin API endpoints
β”‚   β”‚   β”œβ”€β”€ post.routes.js     # Post API endpoints
β”‚   β”‚   β”œβ”€β”€ comment.routes.js  # Comment API endpoints
β”‚   β”‚   β”œβ”€β”€ like.routes.js     # Like API endpoints
β”‚   β”‚   └── bookmark.routes.js # Bookmark API endpoints
β”‚   └── utils/
β”‚       └── sendEmail.js       # Email utility functions
└── Frontend/                  # React frontend (in development)

πŸš€ Getting Started

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB
  • Cloudinary account
  • Gmail account for email functionality

Installation

  1. Clone the repository

    git clone <repository-url>
    cd BlogApi
  2. Install backend dependencies

    cd Backend
    npm install
  3. Environment Setup Create a .env file in the Backend directory:

    PORT=9000
    MONGODB_URI=your_mongodb_connection_string
    JWT_SECRET=your_jwt_secret_key
    CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
    CLOUDINARY_API_KEY=your_cloudinary_api_key
    CLOUDINARY_API_SECRET=your_cloudinary_api_secret
    MAIL_USER=your_gmail_address
    MAIL_PASS=your_gmail_app_password
  4. Start the development server

    npm run dev

The server will start on http://localhost:9000

πŸ“‘ API Endpoints

User Routes (/api/v1/user)

Authentication

  • POST /register - Register a new user
  • POST /login - User login
  • POST /logout - User logout

Profile Management

  • GET /profile - Get user profile
  • PUT /edit-profile - Edit user profile
  • POST /upload-profile - Upload profile image
  • PUT /update-profile-image - Update profile image
  • DELETE /delete-profile - Delete profile image
  • DELETE /delete - Delete user account
  • GET /profile/:username - Get public profile
  • GET /login-history - Get login history

Bio Management

  • POST /bio - Add user bio
  • PUT /bio - Update user bio

Email Management

  • POST /add-email - Add email to account
  • GET /verify-email - Verify email with OTP
  • POST /change-email - Change email address
  • GET /cancel-email - Cancel email verification

Password Management

  • POST /change-password - Change password
  • POST /forget-password - Initiate password reset
  • POST /verify-forget-password - Verify password reset
  • POST /cancel-forget - Cancel password reset

Post Routes (/api/v1/post)

  • GET / - Get all posts
  • GET /:id - Get specific post
  • POST /create - Create new post
  • PUT /update/:id - Update post
  • DELETE /del/:id - Delete post

Comment Routes (/api/v1/comment)

  • POST /:id - Create comment on post
  • GET /:id - Get comments for post
  • PUT /:id - Edit comment
  • DELETE /:id - Delete comment

Like Routes (/api/v1/like)

  • POST /:id - Toggle like on post

Bookmark Routes (/api/v1/bookmark)

  • POST /:id - Toggle bookmark on post
  • GET / - Get bookmarked posts

Admin Routes (/api/v1/admin)

  • GET /dashboard - Admin dashboard statistics
  • GET /users - Get all users
  • GET /users/verified - Get verified users
  • GET /user/:id - Get user details
  • DELETE /user/:id - Delete user
  • GET /posts - Get all post titles
  • GET /posts/:id - Get post details
  • DELETE /post/:id - Delete post

πŸ” Authentication

The API uses JWT (JSON Web Tokens) for authentication. Protected routes require a valid token in the request headers or cookies.

Token Format

Authorization: Bearer <token>

πŸ“§ Email Features

The application includes comprehensive email functionality:

  • Email verification with OTP codes
  • Password reset emails
  • Secure email change process
  • Configurable email templates

πŸ–ΌοΈ Image Upload

Images are stored using Cloudinary:

  • Automatic image optimization
  • Secure cloud storage
  • Public ID tracking for cleanup
  • Support for multiple image formats
  • Separate storage for profile and post images

πŸ”’ Security Features

  • Password Hashing: All passwords are hashed using bcrypt
  • JWT Tokens: Secure authentication tokens
  • Input Validation: Request data validation
  • CORS Protection: Configured cross-origin requests
  • Rate Limiting: Protection against abuse
  • Secure Cookies: HTTP-only cookies for token storage
  • Role-based Access: Admin and user permissions

🚧 Development Status

βœ… Completed Features

  • Backend API (100% Complete)
    • User authentication system
    • Profile management with image upload
    • Email verification system
    • Password reset functionality
    • Blog post CRUD operations
    • Comment system
    • Like system
    • Bookmark system
    • Admin dashboard and management
    • Database models and schemas
    • Complete API route structure
    • Security implementations

🚧 In Progress

  • Frontend development
  • UI/UX design
  • Real-time features

πŸ“‹ Planned Features

  • Real-time notifications
  • User following system
  • Advanced search functionality
  • Mobile app development
  • Analytics dashboard
  • Content moderation tools

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the ISC License.

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

Sheraz Hussain

πŸ“ž Support

For support and questions, please contact the development team or create an issue in the repository.


Note: The backend API is now complete with all core features implemented. The frontend is currently under development.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors