A modern, real-time chat application powered by Google's Gemini 2.5 Flash AI model. Built with Next.js 15, tRPC, and Supabase for a seamless conversational experience.
- π Real-time AI Conversations - Chat with Google's Gemini 2.5 Flash model
- π Secure Authentication - Email/password authentication via Supabase
- π¬ Message History - Persistent chat history for each user
- π Dark Mode - Toggle between light and dark themes
- π± Responsive Design - Optimized for mobile, tablet, and desktop
- β‘ Type-safe API - End-to-end type safety with tRPC
- π¨ Modern UI - Clean, intuitive interface with Tailwind CSS
- Next.js 15 - React framework with App Router
- React 19 - Latest React features
- TypeScript - Type-safe development
- Tailwind CSS 4 - Utility-first CSS framework
- Lucide React - Beautiful icons
- tRPC - End-to-end typesafe APIs
- Supabase - Authentication and database
- Google Gemini API - AI model integration
- TanStack Query - Data fetching and caching
- tRPC React Query - Type-safe React Query integration
Before you begin, ensure you have:
- Node.js 18+ installed
- A Supabase account (sign up here)
- A Google Gemini API key (get one here)
git clone https://github.com/yourusername/ai-chat-app.git
cd ai-chat-appnpm installCreate a .env.local file in the root directory:
# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
# Gemini API Configuration
GEMINI_API_KEY=your_gemini_api_keyWhere to find these:
- Supabase: Dashboard β Project Settings β API
- Gemini API Key: Google AI Studio
Run this SQL in your Supabase SQL Editor:
-- Create messages table
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
model_tag TEXT NOT NULL,
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'error')),
content TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Create indexes for better performance
CREATE INDEX idx_messages_user_id ON messages(user_id);
CREATE INDEX idx_messages_created_at ON messages(created_at);
-- Enable Row Level Security
ALTER TABLE messages ENABLE ROW LEVEL SECURITY;
-- Allow users to view their own messages
CREATE POLICY "Users can view their own messages"
ON messages FOR SELECT
USING (auth.uid() = user_id);
-- Allow users to insert their own messages
CREATE POLICY "Users can insert their own messages"
ON messages FOR INSERT
WITH CHECK (auth.uid() = user_id);- Go to Supabase Dashboard β Authentication β Providers
- Enable Email provider
- (Optional) Disable email confirmation for testing
npm run devOpen http://localhost:3000 to see your app! π
- Sign Up/Login - Create an account or log in with email and password
- Start Chatting - Type your message in the input field
- AI Response - Gemini 2.5 Flash will respond to your messages
- Toggle Dark Mode - Click the sun/moon icon in the header
- View History - All your conversations are saved automatically
ai-chat-app/
βββ src/
β βββ app/ # Next.js App Router
β β βββ api/trpc/ # tRPC API routes
β β βββ globals.css # Global styles
β β βββ layout.tsx # Root layout
β β βββ page.tsx # Home page
β β βββ providers.tsx # React Query & tRPC providers
β βββ components/ # React components
β β βββ AuthForm.tsx # Login/signup form
β β βββ ChatApp.tsx # Main chat interface
β βββ lib/ # Utility libraries
β β βββ gemini.ts # Gemini API integration
β β βββ supabase/ # Supabase clients
β βββ server/ # tRPC backend
β β βββ routers/ # API routers
β β β βββ chat.ts # Chat endpoints
β β βββ index.ts # Router configuration
β β βββ trpc.ts # tRPC setup
β βββ utils/
β βββ trpc.ts # tRPC client
βββ public/ # Static assets
βββ .env.local # Environment variables (create this)
βββ package.json # Dependencies
βββ tsconfig.json # TypeScript config
# Development
npm run dev # Start development server with Turbopack
# Production
npm run build # Build for production with Turbopack
npm start # Start production server
# Type Checking
tsc --noEmit # Check TypeScript types- Secure email/password authentication
- Session management with Supabase
- Automatic token refresh
- Row-level security for user data
- Real-time message updates
- Typing indicators
- Auto-scroll to latest message
- Message history persistence
- Error handling with user feedback
- Google Gemini 2.5 Flash model
- Fast response times
- Context-aware conversations
- Error recovery
Edit src/components/ChatApp.tsx:
// Change the model constant
const FIXED_MODEL = 'gemini-2.5-flash' // or another Gemini modelEdit src/app/globals.css:
:root {
--background: #ffffff;
--foreground: #171717;
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}The modular architecture makes it easy to extend:
- Add new tRPC routers in
src/server/routers/ - Create new components in
src/components/ - Add utility functions in
src/lib/
- Push your code to GitHub
- Import project in Vercel
- Add environment variables:
NEXT_PUBLIC_SUPABASE_URLNEXT_PUBLIC_SUPABASE_ANON_KEYGEMINI_API_KEY
- Deploy! π
- Netlify: Add environment variables and deploy
- Railway: Compatible with Next.js
- Self-hosted: Use
npm run buildandnpm start