Loading...
Loading...
> Let users create accounts, log in, and securely access your application.
Complete authentication system with email/password, Google OAuth, email verification, password reset, two-factor auth, and protected routes. Session-based auth with 30-day expiry.
DESC: Set these environment variables in .env.local
1$# .env.local23$# Where your app runs4$NEXTAUTH_URL="http://localhost:3000"56$# A random secret for encrypting sessions (generate one below)7$NEXTAUTH_SECRET="your-32-character-secret"
DESC: Run this command to generate a secure secret
1$openssl rand -base64 32
Check if user is logged in and get their info
1// src/app/api/your-route/route.ts23import { auth } from "@/lib/auth";4import { NextResponse } from "next/server";56export async function GET() {7 // Get the current user's session8 const session = await auth();910 // Check if they're logged in11 if (!session?.user) {12 return NextResponse.json(13 { error: "You must be logged in" },14 { status: 401 }15 );16 }1718 // User is logged in - you can access their info19 const userId = session.user.id;20 const userEmail = session.user.email;2122 return NextResponse.json({23 message: "Hello!",24 userId25 });26}
Show different content based on login status
1"use client";23import { useSession, signIn, signOut } from "next-auth/react";45export function UserStatus() {6 // Get session data and loading state7 const { data: session, status } = useSession();89 // Show loading while checking auth10 if (status === "loading") {11 return <p>Loading...</p>;12 }1314 // User is logged in15 if (session) {16 return (17 <div>18 <p>Welcome, {session.user?.email}!</p>19 <button onClick={() => signOut()}>20 Log out21 </button>22 </div>23 );24 }2526 // User is not logged in27 return (28 <button onClick={() => signIn()}>29 Log in30 </button>31 );32}
Check auth status in Next.js Server Components
1// No "use client" - this runs on the server23import { auth } from "@/lib/auth";45export default async function PrivatePage() {6 const session = await auth();78 if (!session) {9 return <p>Please log in to view this page.</p>;10 }1112 return (13 <div>14 <h1>Welcome, {session.user?.name}!</h1>15 <p>This is your private dashboard.</p>16 </div>17 );18}
Authentication is how your app knows who someone is. When a user creates an account and logs in, your app gives them a "pass" (called a session) that proves their identity. This pass gets checked every time they access protected areas of your app.
Think of it like a hotel key card - you check in once (log in), get your key card (session), and use it to access your room (protected pages) without re-checking in every time.
Let users sign in with their Google account. This is convenient for users and often increases signup rates.
http://localhost:3000/api/auth/callback/googleGOOGLE_CLIENT_ID="your-client-id.apps.googleusercontent.com"GOOGLE_CLIENT_SECRET="your-client-secret"Fabrk automatically protects these routes:
/dashboard/* - Main user dashboard/settings/* - User settings pages/billing/* - Payment and subscription pages/admin/* - Admin-only pages[ERROR]: NEXTAUTH_SECRET missing
Solution: Generate a secure secret key
# Run in terminal
openssl rand -base64 32
# Add to .env.local
NEXTAUTH_SECRET="output-from-command-above"[ERROR]: Callback URL mismatch
Solution: Verify NEXTAUTH_URL matches your deployed URL
# Development
NEXTAUTH_URL="http://localhost:3000"
# Production (in Vercel environment variables)
NEXTAUTH_URL="https://yourdomain.com"[ERROR]: Session not persisting
Solution: Check DATABASE_URL is correct and database is accessible
# Test database connection
npm run db:studio
# Reset database if needed
npm run db:reset[ERROR]: Google OAuth fails with redirect_uri_mismatch
Solution: Add correct redirect URI in Google Cloud Console
# Development
http://localhost:3000/api/auth/callback/google
# Production
https://yourdomain.com/api/auth/callback/google
# Add both in: Google Cloud Console > Credentials > OAuth 2.0 Client