Loading...
Loading...
Add authentication to library templates with NextAuth v5, session management, and protected routes.
Fabrk uses NextAuth v5 (Auth.js) for authentication. All templates assume authenticated users and need session data for features like user profiles, protected routes, and personalized content.
[WHAT YOU'LL INTEGRATE]:
Fabrk already includes NextAuth v5. Verify your setup:
1$# Check NextAuth is installed2$npm list next-auth34$# Should show: next-auth@5.0.0-beta.x56$# Verify config files exist7$ls -la src/lib/auth.ts8$ls -la src/app/api/auth/[...nextauth]/route.ts
[NOTE] Already Setup?
If you're using Fabrk boilerplate, NextAuth is already configured in src/lib/auth.ts. Skip to Step 2.
Add required environment variables to .env.local:
1$# NextAuth Configuration2$NEXTAUTH_URL="http://localhost:3000"3$NEXTAUTH_SECRET="your-32-character-secret-here"45$# OAuth Providers (optional)6$GOOGLE_CLIENT_ID="your-google-client-id"7$GOOGLE_CLIENT_SECRET="your-google-client-secret"89$GITHUB_CLIENT_ID="your-github-client-id"10$GITHUB_CLIENT_SECRET="your-github-client-secret"
[GENERATE SECRET]:
1$# Generate secure random secret2$openssl rand -base64 32
Server Components (Recommended):
1import { auth } from "@/lib/auth";23export default async function DashboardPage() {4 const session = await auth();56 if (!session?.user) {7 redirect("/auth/sign-in");8 }910 return (11 <div>12 <h1>Welcome, {session.user.name}!</h1>13 <p>Email: {session.user.email}</p>14 </div>15 );16}
Client Components:
1"use client";23import { useSession } from "next-auth/react";45export default function ProfileCard() {6 const { data: session, status } = useSession();78 if (status === "loading") {9 return <div>Loading...</div>;10 }1112 if (!session?.user) {13 return <div>Not authenticated</div>;14 }1516 return (17 <div>18 <h2>{session.user.name}</h2>19 <p>{session.user.email}</p>20 </div>21 );22}
Use middleware to protect entire route groups:
1// middleware.ts (root level)2export { auth as middleware } from "@/lib/auth";34export const config = {5 matcher: [6 "/dashboard/:path*", // Protect all dashboard routes7 "/settings/:path*", // Protect settings8 "/api/user/:path*", // Protect user API routes9 ],10};
Or protect individual pages:
1// app/(dashboard)/analytics/page.tsx2import { auth } from "@/lib/auth";3import { redirect } from "next/navigation";45export default async function AnalyticsPage() {6 const session = await auth();78 if (!session?.user) {9 redirect("/auth/sign-in");10 }1112 // Render protected content13 return <AnalyticsDashboard user={session.user} />;14}
User Avatar Display:
1<Avatar>2 <AvatarImage src={session.user.image ?? undefined} />3 <AvatarFallback>4 {session.user.name?.charAt(0).toUpperCase()}5 </AvatarFallback>6</Avatar>
Sign Out Button:
1import { signOut } from "@/lib/auth";23<form action={async () => {4 "use server";5 await signOut();6}}>7 <button type="submit">> SIGN OUT</button>8</form>
Role-Based Access:
1const session = await auth();2if (session?.user?.role !== "ADMIN") {3 redirect("/dashboard");4}56// Render admin-only template7return <AdminPanel />;
[ERROR]: Session is null
Fix: User not authenticated or session expired. Redirect to sign-in page and verify NEXTAUTH_SECRET is set.
[ERROR]: useSession is not a function
Fix: Missing SessionProvider wrapper. Wrap app in <SessionProvider> in root layout.
[ERROR]: Middleware redirect loop
Fix: Sign-in page is protected by middleware. Exclude auth routes from middleware matcher.