A complete authentication starter template built with Next.js 15, Better Auth, Prisma ORM, PostgreSQL, and Nodemailer. This template provides all the essential authentication features you need to get started quickly with your next project.
- Email & Password - Traditional signup/login with email verification
- Magic Link - Passwordless authentication via email
- OAuth - Social login with Google and GitHub
- Admin Panel - User management with role-based access
- Email Verification - Verify email addresses on signup
- Password Reset - Secure password reset flow
- Magic Link Login - One-click login via email
- Custom Email Templates - Beautiful HTML email templates
- User Profiles - Update name, email, and password
- Role-based Access - User and Admin roles with permissions
- Account Linking - Link multiple OAuth accounts
- Session Management - Secure session handling with cookies
- Password Hashing - Argon2 encryption for passwords
- CSRF Protection - Built-in CSRF protection
- Secure Cookies - HTTP-only, secure cookies
- Email Validation - Domain-based email validation
- Name Validation - Custom name validation rules
- Responsive Design - Mobile-first responsive design
- Modern UI - Built with Tailwind CSS and Radix UI
- Toast Notifications - User feedback with Sonner
- Loading States - Proper loading indicators
- Form Validation - Client-side and server-side validation
- Framework: Next.js 15 (App Router)
- Authentication: Better Auth
- Database: PostgreSQL with Prisma ORM
- Styling: Tailwind CSS + Radix UI
- Email: Nodemailer
- TypeScript: Full type safety
- Icons: Lucide React
- Node.js 18+
- PostgreSQL database
- SMTP email service (Gmail, SendGrid, etc.)
- Clone the repository
git clone https://github.com/yourusername/nextjs-better-auth-starter.git
cd nextjs-better-auth-starter
- Install dependencies
npm install
- Environment Setup
Create a
.env.local
file in the root directory:
# Database
DATABASE_URL="postgresql://username:password@localhost:5432/your_db_name"
# Better Auth
BETTER_AUTH_SECRET="your-secret-key-here"
NEXT_PUBLIC_API_URL="http://localhost:3000"
# OAuth Providers
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"
# Email (Nodemailer)
NODEMAILER_HOST="smtp.gmail.com"
NODEMAILER_PORT="587"
NODEMAILER_USER="your-email@gmail.com"
NODEMAILER_PASS="your-app-password"
# Admin Emails (semicolon separated)
ADMIN_EMAILS="admin@example.com;admin2@example.com"
- Database Setup
# Generate Prisma client
npx prisma generate
# Push database schema
npx prisma db push
- Run the development server
npm run dev
Open http://localhost:3000 in your browser.
src/
βββ app/ # Next.js app router
β βββ auth/ # Authentication pages
β βββ admin/ # Admin dashboard
β βββ profile/ # User profile
β βββ api/auth/ # Better Auth API routes
βββ components/ # Reusable components
β βββ ui/ # UI components (shadcn/ui)
β βββ *-form.tsx # Authentication forms
β βββ *.tsx # Other components
βββ actions/ # Server actions
βββ lib/ # Utility functions
β βββ auth.ts # Better Auth configuration
β βββ auth-client.ts # Client-side auth
β βββ prisma.ts # Prisma client
βββ middleware.ts # Route protection
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URI:
http://localhost:3000/api/auth/callback/google
- Go to GitHub Settings > Developer settings > OAuth Apps
- Create a new OAuth App
- Set Authorization callback URL:
http://localhost:3000/api/auth/callback/github
The template supports any SMTP provider. For Gmail:
- Enable 2-Factor Authentication
- Generate an App Password
- Use the app password in
NODEMAILER_PASS
Deploy your PostgreSQL database (recommended: Neon, Supabase, or PlanetScale)
Deploy to Vercel, Netlify, or any platform supporting Next.js:
npm run build
npm start
Update your environment variables with production values.
// middleware.ts
import { auth } from "@/lib/auth";
export default auth((request) => {
if (!request.auth && request.nextUrl.pathname.startsWith("/profile")) {
return Response.redirect(new URL("/auth/login", request.url));
}
});
"use client";
import { useSession } from "@/lib/auth-client";
export function ProfileComponent() {
const { data: session } = useSession();
if (!session) return <div>Not logged in</div>;
return <div>Welcome, {session.user.name}!</div>;
}
import { auth } from "@/lib/auth";
export default async function ProtectedPage() {
const session = await auth.api.getSession({ headers: headers() });
if (!session) redirect("/auth/login");
return <div>Protected content</div>;
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
- Better Auth - Amazing authentication library
- Next.js - The React framework
- Prisma - Next-generation ORM
- Tailwind CSS - Utility-first CSS framework
- Radix UI - Low-level UI primitives
- Special thanks to Khurram Ali (GiraffeReactor) for his amazing tutorial that helped build this project. His detailed walkthrough and guidance were invaluable.