Skip to content
Back to Templates

MongoDB Atlas Forum

A modern full-stack React template with Next.js, MongoDB, and Better Auth. Ships with a Hacker News-style forum and authentication.

MongoDB News Thumbnail

-> View demo: mongodb-news-template-nextjs.vercel.app

Next.js with MongoDB and Better Auth

A modern full-stack template for building React applications using Next.js, MongoDB, and Better Auth for authentication. Features a Hacker News-style post submission and voting system with optimistic updates, server actions, and email/password authentication.

Features

  • πŸ” Authentication: Email/password authentication with Better Auth
  • πŸ“ Post System: Hacker News-style post submission and voting
  • ⚑ Optimistic Updates: Real-time UI updates with React's useOptimistic
  • 🎨 Modern UI: Built with shadcn/ui and Tailwind CSS
  • πŸ“± Responsive Design: Mobile-first design with dark mode support
  • πŸ—„οΈ MongoDB Integration: Native MongoDB driver with optimized queries
  • πŸ”„ Server Actions: Form handling without client-side API calls
  • πŸ“Š Real-time Status: Database connection monitoring
  • πŸš€ Production Ready: TypeScript, ESLint, and Vercel deployment optimized

Getting Started

Click the "Deploy" button to clone this repo, create a new Vercel project, setup the MongoDB integration, and provision a new MongoDB database:

Local Setup

Installation

Install the dependencies:

npm install

Development

Create a .env file in the project root
cp .env.example .env
Configure environment variables

Update the .env file with your configuration:

# MongoDB connection string from Atlas Dashboard
MONGODB_URI=mongodb+srv://<username>:<password>@<cluster-url>/<database>?retryWrites=true&w=majority
MONGODB_DB=better-auth
# Better Auth configuration
BETTER_AUTH_URL=http://localhost:3000
BETTER_AUTH_SECRET=your-secure-secret-key-here-minimum-32-characters
Setup database indexes (optional for production)
npm run setup-indexes
Start the development server
npm run dev

Open http://localhost:3000 with your browser to see the result.

Tech Stack

Project Structure

β”œβ”€β”€ app/ # Next.js App Router
β”‚ β”œβ”€β”€ api/auth/ # Better Auth API routes
β”‚ β”œβ”€β”€ login/ # Login page
β”‚ β”œβ”€β”€ signup/ # Signup page
β”‚ └── page.tsx # Home page
β”œβ”€β”€ components/ # React components
β”‚ β”œβ”€β”€ ui/ # shadcn/ui components
β”‚ β”œβ”€β”€ PostItem.tsx # Individual post with voting
β”‚ β”œβ”€β”€ PostListServer.tsx # Server-side post list
β”‚ β”œβ”€β”€ PostSection.tsx # Post section wrapper
β”‚ └── ... # Other components
β”œβ”€β”€ lib/ # Core utilities
β”‚ β”œβ”€β”€ auth.ts # Better Auth configuration
β”‚ β”œβ”€β”€ auth-client.ts # Client-side auth methods
β”‚ β”œβ”€β”€ mongodb.ts # Database connection
β”‚ β”œβ”€β”€ posts.ts # Post data functions
β”‚ β”œβ”€β”€ actions.ts # Server actions
β”‚ └── schemas.ts # Zod validation schemas
└── scripts/ # Utility scripts
└── setup-indexes.mjs # Database index setup

Key Features Explained

Authentication System

  • Email/Password: Secure login with email and password
  • Form Validation: Password requirements and email validation
  • Session Management: Secure session handling with MongoDB storage
  • User Profiles: User account management with Better Auth

Post Management

  • Real-time Voting: Optimistic UI updates using React's useOptimistic
  • Server Actions: Form submissions without client-side API calls
  • Caching: Strategic caching with Next.js unstable_cache and revalidation
  • Pagination: URL-based pagination with smooth navigation

Modern React Patterns

  • Server Components: SEO-friendly server-side rendering
  • Client Components: Interactive elements with proper hydration boundaries
  • Suspense: Loading states for better user experience
  • Form Validation: Type-safe forms with React Hook Form and Zod

Learn More

To learn more about the technologies used in this template:

Deploy on Vercel

Commit and push your code changes to your GitHub repository to automatically trigger a new deployment.