A Next.js application with Better Auth authentication system featuring Google OAuth and email/password authentication.
- ✅ Better Auth Integration: Modern authentication library for Next.js
- 🔐 Google OAuth: Sign in with Google account
- 📧 Email/Password Auth: Traditional email and password authentication
- 🛡️ Route Protection: All routes protected except home page
- 🎨 Shadcn UI: Beautiful and consistent UI components
- 📱 Responsive Design: Works on desktop and mobile
- 🌙 Dark Mode Support: Light and dark theme support
- Node.js 18+
- MongoDB (local or cloud instance)
- Google OAuth credentials
pnpm installMake sure you have MongoDB running locally on mongodb://localhost:27017 or set up a cloud MongoDB instance.
The database collections will be automatically created by Better Auth when you first run the application.
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable Google OAuth API
- Go to Credentials > Create Credentials > OAuth 2.0 Client ID
- Set application type to "Web application"
- Add authorized redirect URI:
http://localhost:3000/api/auth/callback/google - Copy the Client ID and Client Secret
Copy the example environment file:
cp .env.example .env.localUpdate .env.local with your actual values:
# Better Auth Configuration
BETTER_AUTH_SECRET=your-super-secret-key-at-least-32-characters-long
BETTER_AUTH_URL=http://localhost:3000
# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017/mirrorship
# Google OAuth Configuration
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
# Next.js Configuration
NEXT_PUBLIC_BASE_URL=http://localhost:3000Important: Generate a secure secret key for BETTER_AUTH_SECRET. You can use:
openssl rand -base64 32pnpm devThe app will be available at http://localhost:3000
/- Home page (public, shows auth status)
/dashboard- User dashboard (requires authentication)- All other routes are protected by default
/sign-in- Sign in with Google or email/password/sign-up- Create account with Google or email/password
Better Auth automatically creates the following MongoDB collections:
user- User profiles and informationsession- User sessionsaccount- OAuth accounts and password credentialsverification- Email verification tokens (if enabled)
- Framework: Next.js 15 with App Router
- Authentication: Better Auth
- Database: MongoDB with official MongoDB adapter
- UI Library: Shadcn UI + Radix UI
- Styling: Tailwind CSS
- State Management: Better Auth's built-in state management
- Notifications: Sonner toast notifications
├── app/
│ ├── api/auth/[...all]/route.ts # Auth API routes
│ ├── sign-in/page.tsx # Sign in page
│ ├── sign-up/page.tsx # Sign up page
│ ├── dashboard/page.tsx # Protected dashboard
│ ├── layout.tsx # Root layout with Toaster
│ └── page.tsx # Home page
├── lib/
│ ├── auth-client.ts # Auth client configuration
│ └── mongodb.ts # MongoDB connection
├── components/ui/ # Shadcn UI components
├── auth.ts # Better Auth configuration
└── middleware.ts # Route protection middleware
- Route Protection: Middleware redirects unauthenticated users
- Secure Sessions: Server-side session management
- CSRF Protection: Built-in CSRF protection
- Password Security: Scrypt password hashing
- OAuth Security: Secure Google OAuth implementation
Edit auth.ts:
socialProviders: {
google: { /* existing config */ },
github: {
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
},
// Add more providers
}Edit middleware.ts to change which routes require authentication.
All UI components are in components/ui/ and can be customized using Tailwind CSS.
- Set production environment variables
- Use a production MongoDB instance
- Update
BETTER_AUTH_URLandNEXT_PUBLIC_BASE_URLto your domain - Update Google OAuth redirect URI to your production domain
- Use a secure
BETTER_AUTH_SECRET
- MongoDB Connection: Ensure MongoDB is running and URI is correct
- Google OAuth: Check client ID, secret, and redirect URI configuration
- Environment Variables: Make sure all required env vars are set
- CORS Issues: Ensure
BETTER_AUTH_URLmatches your deployment URL
Set NODE_ENV=development for detailed error messages.
- Fork the repository
- Create a feature branch
- Make your changes
- Test authentication flows
- Submit a pull request
MIT License - see LICENSE file for details.