nextjs-auth-module
v1.1.0
Published
Complete authentication module for Next.js with PostgreSQL, JWT, and password reset functionality
Maintainers
Readme
Next.js Auth Module
A complete, production-ready authentication module for Next.js applications with PostgreSQL.
Features
- ✅ User registration with email/password
- ✅ User login with JWT tokens
- ✅ Password reset with email notifications
- ✅ Protected routes
- ✅ PostgreSQL database support (Neon, AWS RDS, etc.)
- ✅ JWT authentication
- ✅ Bcrypt password hashing
- ✅ Ready-to-use components
- ✅ TypeScript ready (including .d.ts files)
Installation
Quick Setup (Recommended)
1 npx create-next-app@latest my-app
cd my-app
npm install nextjs-auth-module
npx setup-auth
# When prompted:
# ✓ Would you like to use TypeScript? … No
# ✓ Would you like to use ESLint? … Yes
# ✓ Would you like to use Tailwind CSS? … Yes
# ✓ Would you like to use `src/` directory? … No
# ✓ Would you like to use App Router? … Yes
# ✓ Would you like to customize the default import alias (@/*)? … No
# Navigate into the new project
cd my-new-auth-app
3. # Install all authentication dependencies
npm install bcryptjs jsonwebtoken pg nodemailer @heroicons/react dotenv
# Install dev dependencies (if needed)
npm install -D @types/bcryptjs @types/jsonwebtoken
4. # Install the auth module package
npm install nextjs-auth-package
# Verify it worked
npm list --depth=0 | grep nextjs-auth
# Should show: [email protected]
5. # Run the setup to copy all auth files
npx setup-auth
# Or if that doesn't work, run:
node node_modules/nextjs-auth-package/bin/setup-auth.js
6. 7 . create database
run schema.sql file on sql editor on neon
7. # Create .env.local from example
copy file from .env.example to .env.local
and edit en.local to real data such as db URL and all place holder file with real data
# To generate JWT secret
run in bash
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
8 . npm run dev
Open http://localhost:3000
📁 Project Structure
text
your-project/
├── app/
│ ├── api/
│ │ ├── login/route.js
│ │ ├── register/route.js
│ │ ├── forgot-password/route.js
│ │ └── reset-password/route.js
│ ├── login/page.jsx
│ ├── register/page.jsx
│ ├── forgot-password/page.jsx
│ ├── reset-password/page.jsx
│ └── layout.jsx
├── context/
│ └── AuthContext.jsx
├── lib/
│ ├── db.js
│ ├── auth.js
│ └── email.js
├── scripts/
│ └── init-db.js
├── .env.local
└── package.json
📧 Email Configuration (Gmail)
To enable password reset emails:
Enable 2-Step Verification on your Google account
Generate an App Password:
Go to https://myaccount.google.com/apppasswords
Select "Other" and name it "NextJS Auth"
Copy the 16-character password
Update .env.local:
env
EMAIL_USER="[email protected]"
EMAIL_PASS="16-character-app-password"
🧪 Development Without Email
For testing without email, the reset link will be printed in your terminal:
text
🔐 ========== PASSWORD RESET LINK ==========
Reset URL: http://localhost:3000/reset-password?token=abc123
==========================================
🚢 Deployment
Deploy to Vercel
Push your code to GitHub
Import project to Vercel
Add environment variables in Vercel dashboard
Deploy
Environment Variables on Vercel
env
DATABASE_URL=your_neon_postgres_url
JWT_SECRET=your_jwt_secret
[email protected]
EMAIL_PASS=your_app_password
NEXT_PUBLIC_APP_URL=https://your-domain.vercel.app