Skip to content

David1DDT/youtube-clone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

YouTube Clone πŸ“Ή

GitHub Stars GitHub Forks GitHub Issues TypeScript License: MIT

A full-stack YouTube clone built with modern technologies. This project provides a complete video-sharing platform with user authentication, video uploads, and a responsive UI.

main page screenshot video page screenshot


✨ Features

βœ… Full-Stack Architecture - Frontend with Next.js and TypeScript, backend with Express and MongoDB

βœ… User Authentication - Secure login and registration system with JWT

βœ… Video Uploads - Drag-and-drop video uploads with metadata

βœ… Responsive UI - Modern, clean interface using Mantine UI components

βœ… Video Streaming - Efficient video playback with range requests

βœ… Database Integration - MongoDB with TypeGoose for type-safe schema definition

βœ… Password Hashing - Secure password storage with Argon2

βœ… Real-time Notifications - Using Mantine Notifications for user feedback

βœ… Search Functionality - Coming soon (roadmap)

βœ… Video Recommendations - Coming soon (roadmap)


πŸ› οΈ Tech Stack

Frontend

  • Framework: Next.js 16 (App Router)
  • Language: TypeScript
  • UI Library: Mantine UI (v8)
  • State Management: React Context API
  • Styling: CSS
  • Icons: Tabler Icons

Backend

  • Framework: Express.js
  • Database: MongoDB
  • ORM: TypeGoose
  • Authentication: JWT (JSON Web Tokens)
  • Password Hashing: Argon2
  • Validation: Zod
  • Logging: Pino

DevOps

  • Build Tool: Webpack (via Next.js)
  • Testing: Coming soon (Jest/React Testing Library)
  • CI/CD: Coming soon (GitHub Actions)

πŸ“¦ Installation

Prerequisites

Before you begin, ensure you have the following installed:

Quick Start

  1. Clone the repository:

    git clone https://github.com/David1DDT/youtube-clone.git
    cd youtube-clone
  2. Install dependencies:

    # Install client dependencies
    cd client
    npm install
    
    # Install server dependencies
    cd ../server
    npm install
  3. Set up environment variables: Create a .env file in both the client and server directories with the following variables:

    Client .env:

    NEXT_PUBLIC_API_URL=http://localhost:4000

    Server .env:

    DB_CONNECTION_STRING=mongodb://localhost:27017/youtube-clone
    JWT_SECRET=your_jwt_secret_here
    CORS_ORIGIN=http://localhost:3000
  4. Start the development servers:

    # In a new terminal, start the server
    cd server
    npm dev
    
    # In another terminal, start the client
    cd ../client
    npm dev
  5. Access the application: Open http://localhost:3000 in your browser.


🎯 Usage

Basic Usage

User Authentication

  1. Register a new account: Navigate to /register and fill in the registration form.

  2. Login: Use your credentials to log in at /login.

  3. Upload a Video:

    • Log in to access the upload button.
    • Click the upload button in the header.
    • Fill in the video details (title, description, public visibility).
    • Drag and drop or select a video file.
  4. View Videos:

    • After uploading, your video will appear in the main feed.
    • All public videos will be visible to everyone.

Example Code Snippets

Uploading a Video (Frontend)

// Example of how the upload form works in the Upload.tsx component
const submitHandler = async (e: React.FormEvent) => {
  e.preventDefault();
  if (!fileRef.current?.files?.[0]) return;

  const file = fileRef.current.files[0];
  const formData = new FormData();
  formData.append("file", file);

  const response = await fetch("http://localhost:4000/api/videos", {
    method: "POST",
    body: formData,
    credentials: "include",
  });

  if (!response.ok) throw new Error("Upload failed");

  const data = await response.json();
  console.log("Uploaded video:", data);

  // Update video metadata
  const updateVideoResponse = await fetch(
    `http://localhost:4000/api/videos/${data.videoId}`,
    {
      method: "PATCH",
      credentials: "include",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        title: titleRef.current?.value,
        description: descriptionRef.current?.value,
        published: publicRef.current?.checked,
      }),
    }
  );

  context?.toggle();
  window.location.reload();
};

Video Streaming (Backend)

// Example from video.controller.ts for streaming video chunks
export async function streamVideoHandler(req: Request, res: Response) {
  const { videoId } = req.params;
  const range = req.headers.range;

  if (!videoId)
    return res.status(StatusCodes.BAD_REQUEST).send("Invalid video Id");

  const video = await findVideo(videoId);
  if (!video) {
    return res.status(StatusCodes.NOT_FOUND).send("Video not found");
  }

  const filePath = getPath({
    videoId: video.videoId,
    extension: video.extention,
  });

  const fileSize = fs.statSync(filePath).size;
  const chunkSize = CHUNK_SIZE_IN_BYTES;
  const start = parseInt(range.replace(/\D/g, ""));
  const end = Math.min(start + chunkSize, fileSize - 1);

  res.writeHead(StatusCodes.PARTIAL_CONTENT, {
    "Content-Range": `bytes ${start}-${end}/${fileSize}`,
    "Accept-Ranges": "bytes",
    "Content-Length": end - start + 1,
    "Content-Type": "video/mp4",
  });

  const fileStream = fs.createReadStream(filePath, { start, end });
  fileStream.pipe(res);
}

πŸ“ Project Structure

youtube-clone/
β”œβ”€β”€ client/                  # Next.js frontend application
β”‚   β”œβ”€β”€ app/                 # Next.js application routes
β”‚   β”‚   β”œβ”€β”€ (auth)/          # Authentication routes
β”‚   β”‚   β”‚   β”œβ”€β”€ login/       # Login page
β”‚   β”‚   β”‚   └── register/    # Register page
β”‚   β”‚   β”œβ”€β”€ (main)/         # Main application routes
β”‚   β”‚   β”‚   β”œβ”€β”€ layout.tsx   # Main layout
β”‚   β”‚   β”‚   └── page.tsx     # Home page
β”‚   β”‚   β”œβ”€β”€ components/      # Reusable components
β”‚   β”‚   β”‚   β”œβ”€β”€ Header.tsx   # Main header component
β”‚   β”‚   β”‚   β”œβ”€β”€ Sidebar.tsx  # Sidebar navigation
β”‚   β”‚   β”‚   β”œβ”€β”€ Upload.tsx   # Video upload modal
β”‚   β”‚   β”‚   └── VideoCard.tsx # Video card component
β”‚   β”‚   β”œβ”€β”€ globals.css      # Global styles
β”‚   β”‚   └── layout.tsx       # Root layout
β”‚   β”œβ”€β”€ public/              # Static files
β”‚   β”œβ”€β”€ package.json         # Client dependencies and scripts
β”‚   └── tsconfig.json        # TypeScript configuration
β”‚
β”œβ”€β”€ server/                  # Express backend application
β”‚   β”œβ”€β”€ src/                 # Source files
β”‚   β”‚   β”œβ”€β”€ constants.ts     # Application constants
β”‚   β”‚   β”œβ”€β”€ helpers/         # Helper functions
β”‚   β”‚   β”‚   └── omit.ts      # Object property omitting utility
β”‚   β”‚   β”œβ”€β”€ middleware/      # Express middleware
β”‚   β”‚   β”‚   β”œβ”€β”€ deserializeUser.ts # Deserialize user from JWT
β”‚   β”‚   β”‚   └── requireUser.ts   # Require user to be authenticated
β”‚   β”‚   β”œβ”€β”€ modules/         # Application modules
β”‚   β”‚   β”‚   β”œβ”€β”€ auth/        # Authentication module
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ auth.controller.ts # Auth controllers
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ auth.route.ts    # Auth routes
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ auth.schema.ts  # Auth validation schemas
β”‚   β”‚   β”‚   β”‚   └── auth.utils.ts   # Auth utilities
β”‚   β”‚   β”‚   β”œβ”€β”€ user/        # User module
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ user.controller.ts # User controllers
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ user.model.ts    # User model
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ user.route.ts    # User routes
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ user.schema.ts   # User validation schemas
β”‚   β”‚   β”‚   β”‚   └── user.service.ts  # User service functions
β”‚   β”‚   β”‚   └── videos/      # Video module
β”‚   β”‚   β”‚       β”œβ”€β”€ video.controller.ts # Video controllers
β”‚   β”‚   β”‚       β”œβ”€β”€ video.model.ts    # Video model
β”‚   β”‚   β”‚       β”œβ”€β”€ video.route.ts    # Video routes
β”‚   β”‚   β”‚       β”œβ”€β”€ video.schema.ts   # Video validation schemas
β”‚   β”‚   β”‚       └── video.service.ts  # Video service functions
β”‚   β”‚   β”œβ”€β”€ utils/           # Utility functions
β”‚   β”‚   β”‚   β”œβ”€β”€ database.ts   # Database connection utilities
β”‚   β”‚   β”‚   └── logger.ts     # Logger configuration
β”‚   β”‚   └── main.ts          # Main application entry point
β”‚   β”œβ”€β”€ package.json         # Server dependencies and scripts
β”‚   └── tsconfig.json        # TypeScript configuration
β”‚
β”œβ”€β”€ .gitignore              # Git ignore rules
└── README.md               # Project documentation

πŸ”§ Configuration

Environment Variables

Variable Description Default Value
DB_CONNECTION_STRING MongoDB connection string mongodb://localhost:27017/youtube-clone
JWT_SECRET Secret key for JWT signing your_jwt_secret_here
CORS_ORIGIN Allowed origins for CORS (comma-separated) http://localhost:3000
PORT Port for the Express server 4000

Customization Options

  1. Change UI Theme: Modify the Mantine theme configuration in client/app/layout.tsx to change colors, fonts, and other UI elements.

  2. Add New Features:

    • Extend the video.schema.ts to add new video properties.
    • Add new routes in the respective modules (auth, user, videos).
    • Implement new components in the client/app/components directory.
  3. Database Schema: Extend the User and Video models in their respective .model.ts files to add new fields.


🀝 Contributing

We welcome contributions from the community! Here's how you can contribute to this project:

Development Setup

  1. Fork the repository:

    git clone https://github.com/David1DDT/youtube-clone.git
    cd youtube-clone
  2. Create a new branch:

    git checkout -b feature/your-feature-name
  3. Install dependencies:

    # Client
    cd client
    npm install
    
    # Server
    cd ../server
    npm install
  4. Start the development servers:

    # Server
    cd server
    npm dev
    
    # Client
    cd ../client
    npm dev

Code Style Guidelines

  • TypeScript: Use strict type checking and follow TypeScript best practices.
  • ESLint: Follow the existing ESLint configuration for consistent code style.
  • Commit Messages: Use conventional commit messages for better changelog generation.
  • Naming Conventions:
    • Use camelCase for variables and functions.
    • Use PascalCase for classes and components.
    • Use UPPER_CASE for constants.

Pull Request Process

  1. Write Tests: Add tests for new features or bug fixes.
  2. Document: Update the README or add comments to explain your changes.
  3. Submit PR: Open a pull request with a clear description of your changes.
  4. Review: Address any feedback from maintainers.

πŸ“ License

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


πŸ‘₯ Authors & Contributors

Maintainers


πŸ› Issues & Support

Reporting Issues

If you encounter any problems or have feature requests, please open an issue on the GitHub Issues page. Include:

  • A clear description of the issue or feature request.
  • Steps to reproduce the issue (if applicable).
  • Any relevant logs or error messages.
  • Your environment details (Node.js version, MongoDB version, etc.).

FAQ

Q: Can I use this for commercial purposes? A: Yes, this project is licensed under the MIT License, which allows for both personal and commercial use.

Q: How can I add a new feature? A: Start by creating a new branch and adding your feature. Make sure to write tests and update the documentation. Then submit a pull request.


πŸ—ΊοΈ Roadmap

Planned Features

  • Search Functionality: Implement a search bar to find videos by title, description, or tags.
  • Video Comments: Allow users to comment on videos.
  • Video Likes: Enable users to like videos.
  • User Profiles: Create detailed user profiles with upload history.
  • Subscriptions: Allow users to subscribe to channels.
  • Trending Algorithm: Implement a recommendation algorithm for trending videos.
  • Video Playlists: Create and manage playlists.
  • Mobile App: Develop a mobile application using React Native or Flutter.

Known Issues

  • Video Thumbnails: Currently, thumbnails are not generated automatically. This will be addressed in a future update.
  • Performance Optimization: Further optimization of video streaming and database queries is needed for large-scale deployment.
  • Testing: Comprehensive test coverage is needed for critical components.

Future Improvements

  • Advanced Analytics: Add analytics for video views, engagement, and user behavior.
  • Moderation Tools: Implement tools for content moderation and reporting.
  • Monetization: Add features for monetizing videos through ads.
  • Multi-language Support: Enable the application to support multiple languages.
  • Search: Search videos in the application

πŸš€ Get Started Today!

Ready to dive in? Follow the installation steps and start building your own YouTube clone! Whether you're looking to learn new technologies, contribute to an open-source project, or simply explore a full-stack application, this project offers a great starting point.

Join our community, contribute, and let's build something amazing together! πŸš€

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors