-> View demo: mongodb-news-template-nextjs.vercel.app
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.