Skip to content

kakaa2993/ai-coding-template

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Next.js + Firebase Full-Stack Template with AI-First Development

A production-ready template featuring event-driven broker architecture, designed for rapid AI-assisted development. Build full-stack applications with Next.js frontend and Python Firebase Functions backend using parallel AI agents.

πŸš€ Key Features

Architecture & Development

  • πŸ€– AI-First Development - Optimized for multi-agent parallel development
  • πŸ“Š Event-Driven Architecture - Database as single source of truth
  • πŸ“ Built-in AI Rules - PRD, ADR, and task templates for AI agents
  • πŸ”„ Real-time Updates - Firestore subscriptions for automatic UI updates

Frontend (Next.js)

  • πŸ” Firebase Authentication - Email/Password & Google Sign-In
  • πŸ“Š Firestore Database - Real-time NoSQL with subscription hooks
  • πŸ“ Firebase Storage - File upload and management
  • 🎨 Material-UI (MUI) - Modern React components
  • πŸ”„ State Management - React Context for auth
  • πŸ“ TypeScript - Full type safety
  • πŸš€ Next.js 14 - App Router with server components

Backend (Python Firebase Functions)

  • ⚑ Serverless Functions - Auto-scaling Python backend
  • πŸ—οΈ Broker Architecture - Event-driven, decoupled design
  • πŸ“Š DocumentBase Classes - Structured Firestore management
  • πŸ”’ Authentication Wrappers - Built-in security
  • πŸ§ͺ Integration Testing - Real API testing with emulators
  • πŸ“¦ Pydantic Models - Runtime validation
  • 🎯 TDD Workflow - Test-first development

πŸ“‹ Prerequisites

  • Node.js 18+
  • Python 3.9+
  • Firebase account (sign up at Firebase Console)
  • Firebase CLI (npm install -g firebase-tools)

πŸ”§ Quick Setup

1. Create Firebase Project

  1. Go to Firebase Console
  2. Click "Create a project" or "Add project"
  3. Enter your project name
  4. Choose whether to enable Google Analytics (optional)
  5. Wait for the project to be created

2. Enable Firebase Services

Authentication

  1. Go to Authentication > Sign-in method
  2. Enable Email/Password provider
  3. Enable Google provider (optional but recommended)

Firestore Database

  1. Go to Firestore Database > Create database
  2. Choose Start in test mode for development
  3. Select a location closest to your users
  4. Note: You'll secure it with rules before production

Storage

  1. Go to Storage > Get started
  2. Start in test mode
  3. Choose the same location as your Firestore database

Functions

  1. Go to Functions > Get started
  2. Note: Requires Blaze (Pay as you go) plan
    • Firebase has generous free tiers
    • Required for Functions and advanced features
  3. Follow the setup instructions

3. Get Firebase Configuration

  1. In your Firebase project, click βš™οΈ > Project settings
  2. Scroll to "Your apps" section
  3. Click Web icon </>
  4. Register your app with a nickname
  5. Copy the Firebase configuration object

4. Clone and Setup Project

# Clone the repository
git clone <your-repo-url>
cd <project-directory>

# Install Firebase CLI globally
npm install -g firebase-tools

# Login to Firebase
firebase login

# Configure Firebase project
# Edit .firebaserc and replace with your project ID

πŸ–₯️ Frontend Setup (Next.js)

Navigate to the frontend directory:

cd front

# Install dependencies
yarn install
# or
npm install

# Create environment file
cp .env.example .env.local

Environment Configuration

Open front/.env.local and add your Firebase configuration:

NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key_here
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
NEXT_PUBLIC_FIREBASE_PROJECT_ID=your-project-id
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your-project.appspot.com
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=123456789
NEXT_PUBLIC_FIREBASE_APP_ID=1:123456789:web:abcdef
NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=G-XXXXXXXXXX

Run Frontend Development Server

yarn dev
# or
npm run dev

Visit http://localhost:3000 to see your app.

🐍 Backend Setup (Python Firebase Functions)

Setup Python Environment

cd back

# Create and activate virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Configure Service Account

  1. Go to Firebase Console > Project Settings > Service Accounts
  2. Click "Generate new private key" and download the JSON file
  3. Place the JSON file in the back/ directory
  4. Create .env file in back/ directory:
cp .env.example .env
# Edit .env and add:
# FIREBASE_SERVICE_ACCOUNT_PATH=./your-service-account-key.json
# Add any other API keys your project needs

Backend Development Workflow

# Start Firebase emulators (required for testing)
firebase emulators:start

# In another terminal, run tests
cd back
source venv/bin/activate

# Run all tests with emulators (recommended)
python run_tests.py

# Run only unit tests (fast, no emulator needed)
python run_tests.py --test-type unit --no-emulator

# Run only integration tests
python run_tests.py --test-type integration

# Manual testing with pytest
pytest  # Ensure emulators are running first

Deploy Functions

# Deploy functions only
firebase deploy --only functions

# Deploy security rules
firebase deploy --only firestore:rules,storage:rules

# Deploy everything
firebase deploy

πŸ“ Project Structure

nextjs-firebase-ai-coding-template/
β”œβ”€β”€ front/                         # Next.js Frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ app/                   # Next.js app router pages
β”‚   β”‚   β”‚   β”œβ”€β”€ layout.tsx         # Root layout
β”‚   β”‚   β”‚   β”œβ”€β”€ page.tsx           # Home page
β”‚   β”‚   β”‚   β”œβ”€β”€ signin/            # Sign in page
β”‚   β”‚   β”‚   β”œβ”€β”€ signup/            # Sign up page
β”‚   β”‚   β”‚   └── dashboard/         # Protected dashboard
β”‚   β”‚   β”œβ”€β”€ auth/                  # Authentication logic
β”‚   β”‚   β”‚   β”œβ”€β”€ authContext.ts     # Auth context definition
β”‚   β”‚   β”‚   β”œβ”€β”€ authOperations.ts  # Auth functions
β”‚   β”‚   β”‚   β”œβ”€β”€ AuthProvider.tsx   # Auth context provider
β”‚   β”‚   β”‚   └── useAuth.ts         # Auth hook
β”‚   β”‚   β”œβ”€β”€ lib/                   # Firebase services
β”‚   β”‚   β”‚   β”œβ”€β”€ firebase.ts        # Firebase initialization
β”‚   β”‚   β”‚   β”œβ”€β”€ firestore.ts       # Firestore operations
β”‚   β”‚   β”‚   β”œβ”€β”€ functions.ts       # Cloud Functions
β”‚   β”‚   β”‚   └── storage.ts         # Storage operations
β”‚   β”‚   β”œβ”€β”€ theme/                 # MUI theme configuration
β”‚   β”‚   └── config.ts              # App configuration
β”‚   β”œβ”€β”€ .env.example              # Environment variables template
β”‚   β”œβ”€β”€ package.json
β”‚   └── FRONTEND_RULES.md         # Frontend-specific AI rules
β”œβ”€β”€ back/                          # Python Firebase Functions Backend
β”‚   β”œβ”€β”€ main.py                    # Firebase function exports
β”‚   β”œβ”€β”€ run_tests.py              # Test runner with emulator management
β”‚   β”œβ”€β”€ requirements.txt           # Python dependencies
β”‚   β”œβ”€β”€ pytest.ini                # Pytest configuration
β”‚   β”œβ”€β”€ .env.example              # Backend environment template
β”‚   β”œβ”€β”€ BACKEND_RULES.md         # Backend-specific AI rules
β”‚   β”œβ”€β”€ ADR.md                    # Architecture Decision Records
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ apis/                  # Database interfaces and API clients
β”‚   β”‚   β”‚   └── Db.py              # Database singleton
β”‚   β”‚   β”œβ”€β”€ brokers/               # Firebase function handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ callable/          # Client-callable functions
β”‚   β”‚   β”‚   β”œβ”€β”€ https/             # HTTP endpoints
β”‚   β”‚   β”‚   └── triggered/         # Event-triggered functions
β”‚   β”‚   β”œβ”€β”€ documents/             # Firestore document classes
β”‚   β”‚   β”‚   β”œβ”€β”€ DocumentBase.py    # Base document class
β”‚   β”‚   β”‚   └── [collections]/     # Collection-specific classes
β”‚   β”‚   β”œβ”€β”€ models/                # Data models and types
β”‚   β”‚   β”‚   β”œβ”€β”€ firestore_types.py # Firestore document types
β”‚   β”‚   β”‚   β”œβ”€β”€ function_types.py  # Function request/response types
β”‚   β”‚   β”‚   └── util_types.py      # Utility types
β”‚   β”‚   β”œβ”€β”€ services/              # Business logic services
β”‚   β”‚   β”œβ”€β”€ util/                  # Utility functions
β”‚   β”‚   └── exceptions/            # Custom exceptions
β”‚   └── tests/
β”‚       β”œβ”€β”€ conftest.py            # Pytest fixtures
β”‚       β”œβ”€β”€ integration/           # Integration tests
β”‚       └── unit/                  # Unit tests
β”œβ”€β”€ tasks/                         # AI Development Templates
β”‚   β”œβ”€β”€ PRD.md                    # Product Requirements Document template
β”‚   └── TASK_TEMPLATE.md          # Task breakdown template
β”œβ”€β”€ AGENTS.md                     # AI agent instructions
β”œβ”€β”€ firebase.json                  # Firebase configuration
β”œβ”€β”€ firestore.rules               # Firestore security rules
β”œβ”€β”€ storage.rules                 # Storage security rules
└── README.md                     # This file

πŸ” Security Rules

Firestore Rules

Update firestore.rules with appropriate security rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow users to read/write their own documents
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

Storage Rules

Update storage.rules for file security:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /users/{userId}/{allPaths=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

πŸ€– AI-Assisted Development Workflow

1. Project Scoping (GPT-4o or similar)

# Generate PRD from requirements
# Use tasks/PRD.md template
# Ask AI to interview you until it can fill the PRD

# Break down into tasks
# Use tasks/TASK_TEMPLATE.md
# Have AI create specific, actionable tasks with dependencies

2. Parallel Agent Development

Launch multiple AI agents (Cursor, Claude Code, etc.) in parallel:

# Agent 1: Frontend auth
@AGENTS.md @FRONTEND_RULES.md implement [task1.md]

# Agent 2: Backend API
@AGENTS.md @BACKEND_RULES.md implement [task2.md]

# Agent 3: Database models
@AGENTS.md @BACKEND_RULES.md implement [task3.md]

# Agent 4: Integration tests
@AGENTS.md implement integration tests for [feature]

3. Testing Workflow

# Backend: Always write integration tests first
cd back
source venv/bin/activate
python run_tests.py  # Tests with real APIs

# Frontend: Test with emulators
firebase emulators:start
cd front && yarn dev

4. Documentation

After significant changes, update ADR:

# Tell agent to document decisions
"Document the key architectural decisions in back/ADR.md"

πŸ’» Traditional Development Workflow

Frontend Development

cd front
yarn dev          # Start development server
yarn build        # Build for production
yarn lint         # Run linting

Backend Development

cd back
source venv/bin/activate

# Run tests
python run_tests.py                    # All tests with emulators
python run_tests.py --test-type unit   # Unit tests only
python run_tests.py --test-type integration  # Integration tests only

Full-Stack Development

  1. Start Firebase emulators: firebase emulators:start
  2. Frontend terminal: cd front && yarn dev
  3. Backend terminal: cd back && source venv/bin/activate
  4. Make changes and test integration

πŸ“š Usage Examples

Frontend Authentication

import { useAuth } from "@/auth/useAuth";
import { authOperations } from "@/auth/authOperations";

// Using the auth hook
function MyComponent() {
  const { user, loading, isAuthenticated } = useAuth();

  if (loading) return <div>Loading...</div>;
  if (!isAuthenticated) return <div>Please sign in</div>;

  return <div>Welcome, {user.email}!</div>;
}

// Auth operations
await authOperations.signUp(email, password, displayName);
await authOperations.signIn(email, password);
await authOperations.signInWithGoogle();
await authOperations.signOut();

Frontend Firestore Operations

import { userOperations } from "@/lib/firestore";

// Create user document
await userOperations.create(uid, { displayName, email });

// Get user by ID
const user = await userOperations.getById(uid);

// Update user
await userOperations.update(uid, { displayName: "New Name" });

Backend Callable Functions

# src/brokers/callable/example_callable.py
from firebase_functions import https_fn, options
from src.apis.Db import Db
from src.util.db_auth_wrapper import db_auth_wrapper

@https_fn.on_call(
    cors=options.CorsOptions(cors_origins=["*"]),
    ingress=options.IngressSetting.ALLOW_ALL,
)
def example_callable(req: https_fn.CallableRequest):
    uid = db_auth_wrapper(req)
    # Implementation

Backend Document Classes

# src/documents/examples/Example.py
from src.documents.DocumentBase import DocumentBase
from src.apis.Db import Db
from src.models.firestore_types import ExampleDoc

class Example(DocumentBase[ExampleDoc]):
    pydantic_model = ExampleDoc

    def __init__(self, id: str, doc: Optional[dict] = None):
        self._db = Db.get_instance()
        self.collection_ref = self.db.collections["examples"]
        super().__init__(id, doc)

    @property
    def db(self) -> Db:
        if self._db is None:
            self._db = Db.get_instance()
        return self._db

πŸš€ Deployment

Frontend Deployment (Vercel)

  1. Push your code to GitHub
  2. Import your repository on Vercel
  3. Add environment variables in Vercel dashboard
  4. Deploy!

Backend Deployment (Firebase Functions)

cd back

firebase deploy --only functions

Production Configuration

  1. Update Project IDs in back/src/apis/Db.py:
def is_prod_environment(self) -> bool:
    return self.project_id in ["your-prod-project-id"]

def is_dev_environment(self) -> bool:
    return self.project_id in ["your-dev-project-id"]
  1. Deploy Security Rules:
# Review and customize rules first
cat firestore.rules
cat storage.rules

# Deploy rules
firebase deploy --only firestore:rules,storage:rules
  1. Set Firebase Indexes (if needed):
  • Check console for index requirements
  • Click provided links to create indexes

πŸ§ͺ Testing Strategy

Testing Philosophy

  1. Integration First - Test real Firebase Functions, not mocks
  2. Error Cases First - Test failure scenarios before success
  3. Full Verification - Check both API responses and database state
  4. Use Emulators - Never mock Firebase services
  5. End-to-End - Test complete user workflows

AI Testing Guidelines

When working with AI agents:

# Always tell AI to:
1. Write integration tests with real data
2. Run tests and verify they pass
3. Test with actual files/APIs, not mocks
4. Show you where to find generated outputs

Example Test Pattern

def test_feature_integration(self, firebase_emulator, setup):
    # Arrange - Prepare test data
    test_data = {"field": "value"}
    
    # Act - Call real Firebase Function
    response = requests.post(
        f"{firebase_emulator['base_url']}/function_name",
        json={"data": test_data},
        headers={"User-Id": setup.user_id}
    )

    # Assert - Verify response
    assert response.status_code == 200
    result = response.json()["result"]
    
    # Assert - Verify database state
    doc = DocumentClass(result["docId"])
    assert doc.exists
    assert doc.data.field == "value"

🎯 Architecture Principles

Event-Driven Broker Architecture

  • Database as Truth - Firestore is the single source of truth
  • No Direct Responses - Backend saves to DB, frontend subscribes
  • Real-time Updates - UI updates automatically via Firestore hooks
  • Decoupled Design - Frontend and backend communicate through database

Backend Principles

  • DocumentBase Pattern - All Firestore operations through DocumentBase classes
  • No Direct DB Calls - Always use the Db singleton
  • One Class Per File - Maintain clear file organization
  • Broker Pattern - Handlers stay thin, logic in services

Type Safety

  • Pydantic Models - Define all types in models/
  • Runtime Validation - Catch errors early
  • Type as Documentation - Types serve as API contracts

AI Development Principles

  • Parallel Agents - Run multiple agents simultaneously
  • Test-Driven - AI must write and run tests
  • Documentation - Update ADR after major decisions
  • Real APIs - Never let AI mock critical services

πŸ“œ Available Commands

Frontend Commands

cd front
yarn dev         # Start development server
yarn build       # Build for production
yarn start       # Start production server
yarn lint        # Run ESLint
yarn type-check  # Run TypeScript checks

Backend Commands

cd back
python run_tests.py              # Run all tests with emulators
python run_tests.py --test-type unit  # Unit tests only
python run_tests.py --test-type integration  # Integration tests
pytest --cov=src                 # Run with coverage report

Firebase Commands

firebase emulators:start         # Start local emulators
firebase deploy                  # Deploy everything
firebase deploy --only functions # Deploy functions only
firebase deploy --only hosting   # Deploy frontend only
firebase deploy --only firestore:rules  # Deploy Firestore rules

πŸ”§ Environment Variables

Frontend (front/.env.local)

NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_auth_domain
NEXT_PUBLIC_FIREBASE_PROJECT_ID=your_project_id
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your_storage_bucket
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_messaging_sender_id
NEXT_PUBLIC_FIREBASE_APP_ID=your_app_id
NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=your_measurement_id

Environment Variables

Backend (back/.env):

  • FIREBASE_SERVICE_ACCOUNT_PATH - Path to service account JSON
  • [YOUR_API_KEYS] - Any third-party API keys needed

Frontend (front/.env.local):

  • All NEXT_PUBLIC_FIREBASE_* variables from Firebase config
  • Any other public environment variables

Note: Never commit .env files or service account keys to version control!

πŸš€ Quick Start Checklist

  • Create Firebase project
  • Enable Authentication, Firestore, Storage, Functions
  • Download service account key
  • Configure environment variables
  • Install dependencies (front & back)
  • Start emulators
  • Run tests
  • Launch development servers
  • Create PRD with AI
  • Generate tasks from PRD
  • Launch parallel AI agents

πŸ“š Resources

πŸ†˜ Support

For issues and questions, please open an issue on GitHub.


Built for the AI development era with Next.js, Firebase, and Python

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 72.1%
  • TypeScript 27.7%
  • Other 0.2%