┌─────────────────────────────────────────────────────────────┐
│ CLIENT LAYER │
│ ┌─────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Landing │ │ Auth Pages │ │ Dashboard │ │
│ │ Pages │ │ (Login/Reg) │ │ Pages │ │
│ └─────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ NEXT.JS APP ROUTER │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Middleware (Auth, i18n, Rate Limiting) │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ API/ACTIONS LAYER │
│ ┌───────────┐ ┌───────────┐ ┌────────────┐ │
│ │ Server │ │ API │ │ Webhooks │ │
│ │ Actions │ │ Routes │ │ (Stripe) │ │
│ └───────────┘ └───────────┘ └────────────┘ │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ SERVICE LAYER │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Auth │ │ Payments │ │ Email │ │ Other │ │
│ │ (NextAuth)│ │ (Stripe) │ │ (Resend) │ │ Services │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────┐
│ DATA LAYER │
│ ┌────────────────────────────────────────────────────┐ │
│ │ Prisma ORM → MongoDB │ │
│ └────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
Directory Structure Explained
/app - Next.js App Router
Purpose : All routes and pages
Pattern : File-system based routing
Key files :
page.tsx - Route page component
layout.tsx - Shared layout wrapper
loading.tsx - Loading UI
error.tsx - Error boundary
/actions - Server Actions
Purpose : Server-side business logic
Pattern : "use server" directive
Security : Runs on server only, secure by default
/components - React Components
Purpose : Reusable UI components
Structure :
/ui - Base Shadcn components
/auth - Authentication components
Feature-specific components in feature directories
Purpose : Database client and utilities
Pattern : Centralized Prisma instance
/lib - Utilities & Services
Purpose : Helper functions, utilities, and external integrations
Examples : Stripe, Email, Credits, Authentication helpers
Purpose : Reusable React hooks
Pattern : use prefix (e.g., useCurrentUser)
/types - TypeScript Types
Purpose : Shared type definitions
Pattern : Interface and type declarations
/schemas - Validation Schemas
Purpose : Zod schemas for validation
Usage : Forms, API inputs, database validation
User → Login Form → Server Action (login)
→ NextAuth → Prisma → MongoDB
→ Session Created → Redirect to Dashboard
User → Pricing Page → Checkout
→ Stripe Checkout → Payment Success
→ Webhook → Update User Credits
→ Database Updated → User Notified
User → Dashboard Form → Server Action
→ Auth Check → Validate Input → Prisma Query
→ MongoDB → Success Response
→ UI Updated → Confirmation
Key Technologies Integration
Authentication (NextAuth.js v5)
Config : /auth.config.ts and /auth.ts
Providers : Credentials, Google, GitHub
Session : JWT-based
Database : Prisma adapter for MongoDB
Setup : /lib/stripe.ts
Config : /config/stripe.ts
Webhooks : /app/api/webhooks/stripe/route.ts
Events : checkout.session.completed, payment_intent.succeeded
Model : Credit-based (not subscription)
Database (MongoDB + Prisma)
Schema : /prisma/schema.prisma
Client : /db/index.ts
Migrations : Prisma migrate or db push
Models : User, Account, Credit, Purchase, Verification tokens
Templates : /email-templates/
Service : /lib/mail.ts
Usage : Verification, password reset, transactional emails
Internationalization (next-intl)
Config : /i18n/config.ts
Languages : English, German, Arabic
Translations : /translations/*.json
RTL Support : Automatic detection and handling
// /middleware.ts
export default auth ( ( req ) => {
// Auth checks
// Route protection
// Locale handling
} )
JWT verification via NextAuth
CSRF protection (built-in)
Input validation (Zod schemas)
Rate limiting (ready to add)
Secrets in .env.local
Never commit .env files
Use .env.example as template
Performance Considerations
Server vs Client Components
Default to Server Components
Use 'use client' only when needed:
Event handlers
Browser APIs
State management (useState, useEffect)
Hooks usage
Connection pooling (handled by Prisma)
Indexes on frequently queried fields
Select only needed fields
Static pages where possible
Dynamic routes with revalidation
API routes with cache headers
GitHub → Vercel → Production
↓
Preview Deployments
↓
MongoDB Atlas
↓
External Services (Stripe, Resend)
Stateless server actions
JWT-based sessions (no session store)
Database connection pooling
MongoDB indexes for performance
CDN for static assets
Image optimization (Next.js Image)
Console.error for development
Production: Add Sentry or similar
User events tracking
Performance monitoring
Business metrics
Create feature directory in /app
Add server actions in /actions
Create database models if needed
Add UI components
Update navigation
Adding Third-Party Services
Add SDK to dependencies
Create service wrapper in /lib
Add environment variables
Initialize in appropriate location
Document usage
Create in /app/api/[route]/route.ts
Implement HTTP methods
Add authentication
Handle errors
Document in API docs
Credit System Architecture
Initial Credits : New users receive credits on registration (configurable via INITIAL_CREDITS_FOR_NEW)
Purchase Credits : Users buy credit packs via Stripe
Spend Credits : Features deduct credits via spendCredits() function
Track Usage : Purchase history stored for analytics
New User Registration → Initial Credits Added
User Selects Plan → Stripe Checkout
Payment Success → Webhook Triggers
Credits Added to Balance → Purchase Record Created
User Uses Feature → Credits Deducted
Balance Updated → Transaction Logged
/lib/credits.ts - Credit management functions
/context/CreditsContext.tsx - Client-side credit state
/actions/createStripeSession.ts - Checkout creation
/app/api/webhooks/stripe/route.ts - Payment processing
/config/stripe.ts - Pricing configuration