VercelLogotypeVercelLogotype
LoginSign Up

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.

DeployView Demo
Thumbnail for MongoDB news

-> 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

  • Framework: Next.js 15 with App Router
  • Database: MongoDB with native Node.js driver
  • Authentication: Better Auth with email/password
  • Styling: Tailwind CSS with shadcn/ui
  • Forms: React Hook Form with Zod validation
  • Icons: Lucide React
  • Notifications: Sonner
  • Deployment: Vercel

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:

  • Next.js Documentation - learn about Next.js features and API
  • Better Auth Documentation - authentication and session management
  • MongoDB Documentation - database features and APIs
  • shadcn/ui Documentation - component library and design system
  • Tailwind CSS Documentation - utility-first CSS framework

Deploy on Vercel

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

GitHub Repomongodb-developer/nextjs-news-template-mongodb
Use Cases
Starter
Stack
Next.js
Tailwind
Database
MongoDB

Related Templates

MongoDB RAG Application

A RAG chatbot, with LangChain and MongoDB Atlas, with tailored responses and document uploads through AI generation and knowledge retrieval.
MongoDB RAG Application thumbnail

Get Started

  • Templates
  • Supported frameworks
  • Marketplace
  • Domains

Build

  • Next.js on Vercel
  • Turborepo
  • v0

Scale

  • Content delivery network
  • Fluid compute
  • CI/CD
  • Observability
  • AI GatewayNew
  • Vercel AgentNew

Secure

  • Platform security
  • Web Application Firewall
  • Bot management
  • BotID
  • SandboxNew

Resources

  • Pricing
  • Customers
  • Enterprise
  • Articles
  • Startups
  • Solution partners

Learn

  • Docs
  • Blog
  • Changelog
  • Knowledge Base
  • Academy
  • Community

Frameworks

  • Next.js
  • Nuxt
  • Svelte
  • Nitro
  • Turbo

SDKs

  • AI SDK
  • Workflow SDKNew
  • Flags SDK
  • Chat SDK
  • Streamdown AINew

Use Cases

  • Composable commerce
  • Multi-tenant platforms
  • Web apps
  • Marketing sites
  • Platform engineers
  • Design engineers

Company

  • About
  • Careers
  • Help
  • Press
  • Legal
  • Privacy Policy

Community

  • Open source program
  • Events
  • Shipped on Vercel
  • GitHub
  • LinkedIn
  • X
  • YouTube

Loading status…

Select a display theme:
    • AI Cloud
      • AI Gateway

        One endpoint, all your models

      • Sandbox

        Isolated, safe code execution

      • Vercel Agent

        An agent that knows your stack

      • AI SDK

        The AI Toolkit for TypeScript

      • v0

        Build applications with AI

    • Core Platform
      • CI/CD

        Helping teams ship 6Ɨ faster

      • Content Delivery

        Fast, scalable, and reliable

      • Fluid Compute

        Servers, in serverless form

      • Workflow

        Long-running workflows at scale

      • Observability

        Trace every step

    • Security
      • Bot Management

        Scalable bot protection

      • BotID

        Invisible CAPTCHA

      • Platform Security

        DDoS Protection, Firewall

      • Web Application Firewall

        Granular, custom protection

    • Company
      • Customers

        Trusted by the best teams

      • Blog

        The latest posts and changes

      • Changelog

        See what shipped

      • Press

        Read the latest news

      • Events

        Join us at an event

    • Learn
      • Docs

        Vercel documentation

      • Academy

        Linear courses to level up

      • Knowledge Base

        Find help quickly

      • Community

        Join the conversation

    • Open Source
      • Next.js

        The native Next.js platform

      • Nuxt

        The progressive web framework

      • Svelte

        The web’s efficient UI framework

      • Turborepo

        Speed with Enterprise scale

    • Use Cases
      • AI Apps

        Deploy at the speed of AI

      • Composable Commerce

        Power storefronts that convert

      • Marketing Sites

        Launch campaigns fast

      • Multi-tenant Platforms

        Scale apps with one codebase

      • Web Apps

        Ship features, not infrastructure

    • Tools
      • Marketplace

        Extend and automate workflows

      • Templates

        Jumpstart app development

      • Partner Finder

        Get help from solution partners

    • Users
      • Platform Engineers

        Automate away repetition

      • Design Engineers

        Deploy for every idea

  • Enterprise
  • Pricing
Log InContact
Sign Up
Sign Up
DeployView Demo
AI Gateway

One endpoint, all your models

Sandbox

Isolated, safe code execution

Vercel Agent

An agent that knows your stack

AI SDK

The AI Toolkit for TypeScript

v0

Build applications with AI

CI/CD

Helping teams ship 6Ɨ faster

Content Delivery

Fast, scalable, and reliable

Fluid Compute

Servers, in serverless form

Workflow

Long-running workflows at scale

Observability

Trace every step

Bot Management

Scalable bot protection

BotID

Invisible CAPTCHA

Platform Security

DDoS Protection, Firewall

Web Application Firewall

Granular, custom protection

Customers

Trusted by the best teams

Blog

The latest posts and changes

Changelog

See what shipped

Press

Read the latest news

Events

Join us at an event

Docs

Vercel documentation

Academy

Linear courses to level up

Knowledge Base

Find help quickly

Community

Join the conversation

Next.js

The native Next.js platform

Nuxt

The progressive web framework

Svelte

The web’s efficient UI framework

Turborepo

Speed with Enterprise scale

AI Apps

Deploy at the speed of AI

Composable Commerce

Power storefronts that convert

Marketing Sites

Launch campaigns fast

Multi-tenant Platforms

Scale apps with one codebase

Web Apps

Ship features, not infrastructure

Marketplace

Extend and automate workflows

Templates

Jumpstart app development

Partner Finder

Get help from solution partners

Platform Engineers

Automate away repetition

Design Engineers

Deploy for every idea