-> View demo: news.mongodb.com
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.
Click the "Deploy" button to clone this repo, create a new Vercel project, setup the MongoDB integration, and provision a new MongoDB database:
Install the dependencies:
npm install
Configure environment variables
cp .env.example .env
Update the .env
file with your configuration:
Setup database indexes (optional for production)
# MongoDB connection string from Atlas DashboardMONGODB_URI=mongodb+srv://<username>:<password>@<cluster-url>/<database>?retryWrites=true&w=majorityMONGODB_DB=better-auth# Better Auth configurationBETTER_AUTH_URL=http://localhost:3000BETTER_AUTH_SECRET=your-secure-secret-key-here-minimum-32-characters
Start the development server
npm run setup-indexes
npm run dev
Open http://localhost:3000 with your browser to see the result.
├── 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
useOptimistic
unstable_cache
and revalidationTo learn more about the technologies used in this template:
Commit and push your code changes to your GitHub repository to automatically trigger a new deployment.