Next.js 14 Fullstack NextAuth.js & i18n Example (with MongoDB + TypeScript)
A full-stack authentication demo built with Next.js 14, NextAuth.js v5, MongoDB (via Mongoose), and TypeScript.
Supports both Google OAuth and email/password login, with internationalization (i18n) and comprehensive authentication flows.
✨ Features
-
Google OAuth Login – Authenticate users via their Google accounts.
-
Email & Password Authentication: Sign-in with email and password.
-
Email Verification: Ensure account validity by requiring users to confirm their email address.
-
Forgot Password Flow: Allow users to reset their password through a email link.
-
Two-Factor Authentication (2FA): Login requires a digit verification code sent via email.
-
User Account Settings: Let users update their username, change passwords, and enable or disable 2FA.
-
Internationalization (i18n) with Auth Integration: Built-in locale routing with authentication middleware support.
This is a Next.js project bootstrapped with create-next-app.
🚀 Getting Started
Run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
Open http://localhost:3000 with your browser to see the result.
⚙️ Environment Variables
Create a .env file in the project root and include the following:
NEXT_PUBLIC_APP_URL="http://localhost:3000"
AUTH_SECRET="YOUR_AUTH_SECRET"
INTERNAL_API_KEY="YOUR_INTERNAL_API_KEY"
INTERNAL_API_SECRET="YOUR_INTERNAL_API_SECRET"
NEXT_PUBLIC_INTERNAL_API_SECRET="YOUR_NEXT_PUBLIC_INTERNAL_API_SECRET"
MONGODB_URI="YOUR_MONGODB_URI"
GOOGLE_CLIENT_ID="YOUR_GOOGLE_CLIENT_ID"
GOOGLE_CLIENT_SECRET="YOUR_GOOGLE_CLIENT_SECRET"
TOKEN_SECRET="YOUR_TOKEN_SECRET"
# Resend
RESEND_API_KEY="YOUR_RESEND_API_KEY"
RESEND_EMAIL_URL="YOUR_RESEND_EMAIL_URL"
# OR
# Nodemailer
EMAIL_USER="YOUR_EMAIL_USER"
EMAIL_PASSWORD="YOUR_EMAIL_PASSWORD"
🔐 Getting Google OAuth Credentials
How to obtain GOOGLE_CLIENT_ID & GOOGLE_CLIENT_SECRET