A modern, full-stack authentication template built with Next.js, Convex, and Better Auth. This template provides a complete authentication system with email verification, password reset, and 2FA support.
- β Complete Authentication System - Sign up, sign in, email verification, password reset
- β Two-Factor Authentication (2FA) - TOTP-based 2FA support
- β Protected Routes - Automatic route protection with middleware
- β Modern UI - Beautiful, responsive design with dark mode support
- β Type Safety - Full TypeScript support with Convex
- β Real-time Database - Powered by Convex for real-time updates
- β Email Templates - Pre-built email templates for auth flows
Before you begin, ensure you have the following installed:
- Node.js (v18 or later)
- pnpm (recommended) or npm/yarn
- Convex CLI
git clone <your-repo-url>
cd next-convex-betterauth-template
pnpm install
Create a .env.local
file in the root directory with the following variables:
# Convex (automatic)
CONVEX_DEPLOYMENT=automatic
NEXT_PUBLIC_CONVEX_URL=https://example.convex.cloud
NEXT_PUBLIC_CONVEX_SITE_URL=https://example.convex.site
# Site Configuration
SITE_URL=http://localhost:3000
# Google OAuth (optional)
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
# GitHub OAuth (optional)
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
# Better Auth
BETTER_AUTH_SECRET=
After creating your .env.local
file, set the required variables in Convex:
# Set up environment variables in Convex (development)
pnpm convex env set SITE_URL http://localhost:3000
pnpm convex env set BETTER_AUTH_SECRET your-secret-key-here
pnpm convex env set GOOGLE_CLIENT_ID your-google-client-id
pnpm convex env set GOOGLE_CLIENT_SECRET your-google-client-secret
pnpm convex env set GITHUB_CLIENT_ID your-github-client-id
pnpm convex env set GITHUB_CLIENT_SECRET your-github-client-secret
# Set up environment variables in Convex (production)
pnpm convex env set SITE_URL your-production-domain --prod
pnpm convex env set BETTER_AUTH_SECRET your-secret-key-here --prod
pnpm convex env set GOOGLE_CLIENT_ID your-google-client-id --prod
pnpm convex env set GOOGLE_CLIENT_SECRET your-google-client-secret --prod
pnpm convex env set GITHUB_CLIENT_ID your-github-client-id --prod
pnpm convex env set GITHUB_CLIENT_SECRET your-github-client-secret --prod
You'll need to set up the following services:
Option 1: Resend (Recommended)
- Sign up at Resend
- Get your API key from the dashboard
- Add
RESEND_API_KEY
to your environment variables
Option 2: SMTP (Gmail/Outlook)
- For Gmail: Enable 2FA and create an App Password
- For Outlook: Use your regular credentials
- Add SMTP variables to your environment
- Go to Better Auth Installation and click "Generate Secret" to get your key
- Or generate a secure random string (32+ characters) using:
openssl rand -base64 32
- Add to
BETTER_AUTH_SECRET
# This will automatically start both Convex dev and Next.js dev
pnpm dev
# Start Convex development server only
pnpm convex dev
# Or run once and exit
pnpm convex dev --once
Note: If you encounter errors after running pnpm install
after cloning this repo, make sure you have added the required environment variables to your .env.local
file, then run pnpm convex env set
to set up the variables in Convex.
The pnpm dev
command will:
- Start Convex development server
- Start Next.js development server
- Open your browser to
http://localhost:3000
- Sign Up: Create a new account at
/sign-up
- Verify Email: Check your email and click the verification link
- Sign In: Use your credentials at
/sign-in
- Dashboard: Access your protected dashboard at
/dashboard
- Settings: Configure 2FA and other settings at
/settings
src/
βββ app/
β βββ (auth)/ # Protected routes
β β βββ dashboard/ # Main dashboard
β β βββ settings/ # User settings
β βββ (unauth)/ # Public routes
β β βββ sign-in/ # Sign in page
β β βββ sign-up/ # Sign up page
β β βββ verify-2fa/ # 2FA verification
β βββ api/auth/ # Auth API routes
βββ components/ # Reusable components
βββ lib/ # Utility functions
βββ middleware.ts # Route protection
convex/
βββ auth.config.ts # Better Auth configuration
βββ auth.ts # Auth functions
βββ schema.ts # Database schema
βββ emails/ # Email templates
To add new pages to your dashboard:
- Create a new directory in
src/app/(auth)/dashboard/
- Add a
page.tsx
file - Use the provided components:
"use client";
import { useQuery } from "convex/react";
import { api } from "../../../../convex/_generated/api";
import { AppContainer } from "@/components/server";
export default function MyPage() {
const user = useQuery(api.auth.getCurrentUser);
return (
<AppContainer>
<h1>Hello {user?.name}!</h1>
{/* Your page content */}
</AppContainer>
);
}
- Push your code to GitHub
- Connect your repository to Vercel
- Configure build settings:
- Build Command:
npx convex deploy --cmd 'pnpm run build'
- Install Command:
pnpm install
- Build Command:
- Add environment variables in Vercel dashboard:
- All variables from your
.env.local
file - Additional variables for production:
CONVEX_SITE_URL=your-domain
(your production domain)CONVEX_DEPLOYMENT=key from convex dashboard
(available in Convex dashboard under Settings > Deployment URL and Deploy Key)
- All variables from your
- Deploy!
pnpm convex deploy
If you're working with AI coding assistants or need to modify authentication providers:
When adding or removing authentication providers (Google, GitHub, etc.):
- Update Environment Variables: Add or remove the corresponding
CLIENT_ID
andCLIENT_SECRET
variables in both.env.local
and Convex - Check Convex Configuration: Review and update
convex/auth.config.ts
to include/exclude the provider - Update Mutations: Ensure all related Convex mutations and functions are updated to handle the new/removed provider
- Test Authentication Flow: Verify the complete authentication flow works with the changes
- To add a provider: Configure the provider in
convex/auth.config.ts
, add environment variables, and update any related mutations - To remove a provider: Remove from auth config, clean up environment variables, and update mutations to handle the removal
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues:
- Check the Issues page
- Create a new issue with detailed information
- Join our community discussions
Happy coding! :D