Skip to content
harshgharsandiyaPublic

About

Complete NextAuth

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Next Auth

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.

✨ Key Features

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):
    • ADMIN and USER roles.
    • RoleGate component 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 useCurrentUser and useCurrentRole.
    • Flexible LoginButton with "redirect"

🛠 Tech Stack

  • 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

🚀 Getting Started

Follow these steps to get the project up and running on your local machine.

1. Clone the Repository

git clone https://github.com/harshgharsandiya/NextAuth.git
cd NextAuth/nextauth

2. Install Dependencies

npm install

3. Set Up Environment Variables

Create 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"

4. Push the Database Schema

Run the following command to sync your Prisma schema with your database:

npx prisma generate
npx prisma db push

5. Run the Development Server

npm run dev

Your 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.


🤝 Contributing

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".

How to Contribute

  1. Fork the Project (click the "Fork" button in the top-right corner).
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature).
  3. Commit your Changes (git commit -m 'Add some AmazingFeature').
  4. Push to the Branch (git push origin feature/AmazingFeature).
  5. Open a Pull Request against the main branch.

Bug Reports & Feature Requests

Found a bug or have an idea?

We look forward to your contributions

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Complete NextAuth

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages