# NextAuth v5 + Next.js 15 + React 19 + Mongoose Starter 🔥
A production-ready authentication template using NextAuth v5, MongoDB/Mongoose, and TypeScript. It supports both credentials and OAuth providers.
## 🚀 Quick Start
### 1. Clone the repository:
```bash
git clone git@github.com:dharmveer97/next-auth-mongoose.git
cd next-auth-mongoose
npm install
# or
bun install
MONGODB_URI="your_mongodb_connection_string"
NEXTAUTH_SECRET="generated_secret_here"
NEXTAUTH_URL="http://localhost:3000"
GOOGLE_CLIENT_ID="your_google_oauth_id"
GOOGLE_CLIENT_SECRET="your_google_oauth_secret"
openssl rand -base64 32
# or
npx auth secret
npm run dev
# or
bun run dev
- Authentication: NextAuth v5
- Database: MongoDB + Mongoose ODM
- Frontend: React 19 + TypeScript
- Forms: Formik + Yup validation
- Styling: Tailwind CSS
- Security: BcryptJS password hashing
├── app/
│ ├── api/auth/[...nextauth]/route.ts
│ └── (auth)/
├── components/
│ └── Auth/
├── models/
│ └── User.ts
├── lib/
│ └── mongoose.ts
└── types/
└── next-auth.d.ts
- Create a free cluster at MongoDB Atlas.
- Get your connection string:
MONGODB_URI="mongodb+srv://<user>:<password>@cluster.mongodb.net/dbname?retryWrites=true&w=majority"
- Go to Google Cloud Console.
- Create OAuth 2.0 credentials.
- Add the authorized redirect URI:
http://localhost:3000/api/auth/callback/google
import { Schema, model } from 'mongoose';
const UserSchema = new Schema({
name: { type: String },
email: { type: String, unique: true },
password: { type: String, select: false },
role: { type: String, enum: ['user', 'admin'], default: 'user' },
emailVerified: { type: Date, default: null },
}, { timestamps: true });
export const User = model('User', UserSchema);
- Password hashing with bcryptjs
- CSRF protection
- HTTPS-only cookies
- Secure session management
- Environment variable validation
- Type-safe API routes
- Set environment variables in project settings.
- Add the build command:
npm run build
. - Enable Serverless Functions.
# netlify.toml
[build]
command = "npm run build"
publish = ".next"
- Customize the sign-in page in
app/(auth)/#/page.tsx
. - Add more OAuth providers in
[...nextauth]/route.ts
. - Extend the User model with additional fields.
- Use
getServerSession()
for server-side authentication. - Implement rate limiting for authentication endpoints.
Keywords: Next.js Authentication, MongoDB Auth, NextAuth v5 Tutorial, React 19 Starter, TypeScript Auth Template, Mongoose User Model, Google OAuth Integration, Secure Login System