A complete, production-ready authentication template built with Next.js 14 (App Router), NextAuth.js v5 (Auth.js), and Prisma. This project provides a robust foundation for any modern web application requiring secure, flexible, and feature-rich user authentication.
This project includes a wide range of authentication and user management features:
- Credentials Login: Secure email and password login.
- OAuth Providers: Sign in with Google and GitHub.
- User Registration: New user sign-up with password hashing (bcrypt).
- Email Verification: New users receive a verification email to activate their account.
- Password Reset: Secure flow for users to reset their forgotten password via email.
- Two-Factor Authentication (2FA): Users can enable 2FA for an extra layer of security, with email-based codes.
- Role-Based Access Control (RBAC):
ADMINandUSERroles.RoleGatecomponent to protect content based on user role.- Protected server-side and client-side routes.
- Comprehensive Settings Page:
- Update user's name.
- Change email (with a re-verification flow).
- Change password (requires old password).
- Enable/Disable Two-Factor Authentication.
- Update user role (Admin-only).
- OAuth Account Linking: Prevents credentials-based users from using settings (password, email) they don't have.
- Modern Tech:
- Next.js 14 App Router
- Server Actions for all mutations (login, register, settings).
- Optimistic UI updates with
useTransition. - Efficient session management with
useSession().update()to avoid database refetches. - Custom hooks like
useCurrentUseranduseCurrentRole. - Flexible
LoginButtonwith "redirect"
- Framework: Next.js 14 (App Router)
- Authentication: NextAuth.js v5
- ORM: Prisma
- Database: PostgreSQL
- UI: shadcn/ui, Tailwind CSS
- Schema Validation: Zod
- Email Service: NodeMailer + Gmail App Password
- Icons:
lucide-react&react-icons
Follow these steps to get the project up and running on your local machine.
git clone https://github.com/harshgharsandiya/NextAuth.git
cd NextAuth/nextauthnpm installCreate a .env file in the root of your project and add the following variables. See .env.example for a template.
# Database
# Example for PostgreSQL
DATABASE_URL="postgresql://username:password@localhost:5432/your-db-name"
# NextAuth.js
# Generate a secret: openssl rand -base64 32
AUTH_SECRET="your-auth-secret"
# OAuth Providers
# Github Provider
GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"
# Google Provider
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
# Email Service
GMAIL_EMAIL="your-gmail"
GMAIL_APP_PASSWORD="your-gmail-app-password"
# Public App Url
NEXT_PUBLIC_APP_URL="http://localhost:3000"Run the following command to sync your Prisma schema with your database:
npx prisma generate
npx prisma db pushnpm run devYour application should now be running at http://localhost:3000.
Got it! You want to add a section to your README.md to invite other developers to collaborate on your open-source project.
This is a "Contributing" section. Here is the text for it.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this project better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
- Fork the Project (click the "Fork" button in the top-right corner).
- Create your Feature Branch (
git checkout -b feature/AmazingFeature). - Commit your Changes (
git commit -m 'Add some AmazingFeature'). - Push to the Branch (
git push origin feature/AmazingFeature). - Open a Pull Request against the
mainbranch.
Found a bug or have an idea?
- Open an Issue: Click here to open a new issue. Please provide as much detail as possible, including steps to reproduce (for bugs).
We look forward to your contributions
This project is licensed under the MIT License. See the LICENSE file for details.