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.
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.
Before you begin, ensure you have the following installed:
- Node.js: (LTS version recommended) - https://nodejs.org/
- pnpm: (Used as the package manager in this project) -
npm install -g pnpm - Git: https://git-scm.com/
- MongoDB Account: A MongoDB database instance (e.g., a free tier cluster on MongoDB Atlas - https://www.mongodb.com/cloud/atlas). You will need the connection string.
- Google AI API Key: An API key for Google Generative AI (Gemini) - https://ai.google.dev/
-
Clone the Repository:
git clone <repository-url> # Replace <repository-url> with the actual URL cd guruchat-nextjs # Or your repository directory name
-
Install Dependencies: Use pnpm to install the project dependencies.
pnpm install
-
Configure Environment Variables: Create a
.env.localfile 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_URIpoints to the desired database (e.g.,guruchat). - Security Note: Ensure
JWT_SECRETis a strong, unique, and secret key. Do not commit the.env.localfile to version control (it's included in.gitignore).
- Make sure your
-
Seed the Database (if necessary): This step populates the database with the default Guru personas defined in
scripts/seedGurus.ts. Run this command if yourguruscollection 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.
-
Run the Development Server: Start the Next.js development server using Vercel CLI.
vercel dev
This command loads environment variables from
.env.localand starts the server, typically accessible athttp://localhost:3000.
A basic API test script is included to verify backend functionality.
-
Ensure
jqis installed:- macOS:
brew install jq - Windows: Download from https://stedolan.github.io/jq/download/ and ensure it's in your PATH, or use WSL/Git Bash.
- Linux:
sudo apt-get install jqorsudo yum install jq
- macOS:
-
Make the script executable (macOS/Linux):
chmod +x scripts/test-api.sh
-
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.
- Authentication Enforcement: The
middleware.tsfile handles authentication checks for accessing application routes. It verifies ajwt_tokencookie (which should be set via HttpOnly during login). - Behavior:
- Authenticated users attempting to access
/loginor/registerare redirected to the homepage (/). - Unauthenticated users attempting to access any protected route (including
/) are redirected to/login. Access is allowed only to the/login,/registerpages and their corresponding API endpoints (/api/auth/login,/api/auth/register).
- Authenticated users attempting to access
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.