fengshch / next-auth-v5-advanced-guide

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Next Auth v5 - Advanced Guide

Key Features:

  • ๐Ÿ” Next-auth v5 (Auth.js)
  • ๐Ÿš€ Next.js 14 with server actions
  • ๐Ÿ”‘ Credentials Provider
  • ๐ŸŒ OAuth Provider (Social login with Google & GitHub)
  • ๐Ÿ”’ Forgot password functionality
  • โœ‰๏ธ Email verification
  • ๐Ÿ“ฑ Two factor verification
  • ๐Ÿ‘ฅ User roles (Admin & User)
  • ๐Ÿ”“ Login component (Opens in redirect or modal)
  • ๐Ÿ“ Register component
  • ๐Ÿค” Forgot password component
  • โœ… Verification component
  • โš ๏ธ Error component
  • ๐Ÿ”˜ Login button
  • ๐Ÿšช Logout button
  • ๐Ÿšง Role Gate
  • ๐Ÿ” Exploring next.js middleware
  • ๐Ÿ“ˆ Extending & Exploring next-auth session
  • ๐Ÿ”„ Exploring next-auth callbacks
  • ๐Ÿ‘ค useCurrentUser hook
  • ๐Ÿ›‚ useRole hook
  • ๐Ÿง‘ currentUser utility
  • ๐Ÿ‘ฎ currentRole utility
  • ๐Ÿ–ฅ๏ธ Example with server component
  • ๐Ÿ’ป Example with client component
  • ๐Ÿ‘‘ Render content for admins using RoleGate component
  • ๐Ÿ›ก๏ธ Protect API Routes for admins only
  • ๐Ÿ” Protect Server Actions for admins only
  • ๐Ÿ“ง Change email with new verification in Settings page
  • ๐Ÿ”‘ Change password with old password confirmation in Settings page
  • ๐Ÿ”” Enable/disable two-factor auth in Settings page
  • ๐Ÿ”„ Change user role in Settings page (for development purposes only)

Prerequisites

Node version 18.7.x

Cloning the repository

git clone https://github.com/Shahriyar-Hosen/next-auth-v5-advanced-guide.git

Extract this system configuration

1. Install packages

npm | yarn | pnpm
npm i

1.1 Install dependencies and devDependencies

dependencies
npm i next-auth@beta @auth/prisma-adapter resend uuid zod bcryptjs react-hook-form @hookform/resolvers next-themes react-icons react-spinners @radix-ui/react-icons
devDependencies
npm i -d @types/uuid @types/bcryptjs

1.2 Install shadcn-ui

1.3 Install shadcn-ui component

npx shadcn-ui@latest add button card switch form input select sonner dialog dropdown-menu badge avatar

2. all file and folder added

3. Setup .env file

# Database Url
DATABASE_URL="mongodb url"

# Next-Auth secret
AUTH_SECRET=next-auth-secret

# Github and Google id and secret
GITHUB_CLIENT_ID="Google id from google api console"
GITHUB_CLIENT_SECRET="Google secret from google api console"
GOOGLE_CLIENT_ID="Github id from Github -> Settings -> Developer Settings -> OAuth Apps"
GOOGLE_CLIENT_SECRET="Github secret from Github -> Settings -> Developer Settings -> OAuth Apps"

# resend (email provider) api key
RESEND_API_KEY="resend api key"

# base url
NEXT_PUBLIC_APP_URL="http://localhost:3000"

4. Setup Prisma

npx prisma generate
npx prisma db push

5. Start the app

pnpm run dev

Available commands

Running commands with pnpm pnpm run [command]

command description
dev Starts a development instance of the app

About


Languages

Language:TypeScript 98.2%Language:CSS 1.7%Language:JavaScript 0.2%