Skip to content

pranavkafle/guruchat-nextjs

Repository files navigation

GuruChat - Next.js Full-Stack Application

This repository contains the Next.js version of the GuruChat application, implemented using the MNT stack (MongoDB, Next.js, TypeScript) and designed for deployment on Vercel.

Overview

The application allows users to register, log in, and chat with different "Guru" personas powered by Google's Generative AI. Built as a full-stack Next.js application with API routes for the backend and React components for the frontend, leveraging MongoDB for data storage.

Prerequisites

Before you begin, ensure you have the following installed:

Setup Instructions (Windows/macOS)

  1. Clone the Repository:

    git clone <repository-url> # Replace <repository-url> with the actual URL
    cd guruchat-nextjs # Or your repository directory name
  2. Install Dependencies: Use pnpm to install the project dependencies.

    pnpm install
  3. Configure Environment Variables: Create a .env.local file in the root directory of the project. Add the following variables, replacing the placeholder values with your actual credentials:

    MONGODB_URI=your_mongodb_connection_string # e.g., mongodb+srv://user:[email protected]/guruchat
    JWT_SECRET=your_strong_jwt_secret          # A secure random string for signing tokens
    GOOGLE_AI_API_KEY=your_google_ai_api_key   # Your API key from Google AI Studio
    • Make sure your MONGODB_URI points to the desired database (e.g., guruchat).
    • Security Note: Ensure JWT_SECRET is a strong, unique, and secret key. Do not commit the .env.local file to version control (it's included in .gitignore).
  4. Seed the Database (if necessary): This step populates the database with the default Guru personas defined in scripts/seedGurus.ts. Run this command if your gurus collection is empty or if you are setting up the application for the first time.

    pnpm run seed:gurus
    • Note: If your database already contains Guru data, running this script might not be necessary and could potentially lead to duplicate entries or errors if the script isn't designed to handle existing data. Check the script's logic if unsure.
  5. Run the Development Server: Start the Next.js development server using Vercel CLI.

    vercel dev

    This command loads environment variables from .env.local and starts the server, typically accessible at http://localhost:3000.

Running API Tests

A basic API test script is included to verify backend functionality.

  1. Ensure jq is installed:

  2. Make the script executable (macOS/Linux):

    chmod +x scripts/test-api.sh
  3. Run the test script: Make sure the development server (vercel dev) is running in another terminal.

    ./scripts/test-api.sh

    The script will test registration, login, fetching gurus, and initiating a chat stream. It checks HTTP status codes and basic response structures.

Middleware Configuration Notes

  • Authentication Enforcement: The middleware.ts file handles authentication checks for accessing application routes. It verifies a jwt_token cookie (which should be set via HttpOnly during login).
  • Behavior:
    • Authenticated users attempting to access /login or /register are redirected to the homepage (/).
    • Unauthenticated users attempting to access any protected route (including /) are redirected to /login. Access is allowed only to the /login, /register pages and their corresponding API endpoints (/api/auth/login, /api/auth/register).

Deployment

This project is configured for easy deployment on Vercel. Connect your Git repository (GitHub, GitLab, Bitbucket) to Vercel and ensure the necessary environment variables (MONGODB_URI, JWT_SECRET, GOOGLE_AI_API_KEY) are configured in the Vercel project settings. Vercel will automatically detect the Next.js framework and build/deploy the application.

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published