NextAuth v5 setup guide: authentication for your Next.js SaaS
NextAuth v5 is the authentication library used in CheapStack kits. Here's how it works.
Basic setup
NextAuth v5 uses a single configuration file that defines your auth providers and callbacks.
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [GitHub, Google],
callbacks: {
session({ session, user }) {
if (session.user) session.user.id = user.id;
return session;
},
},
});
Route handler
NextAuth v5 uses a catch-all route file for the API endpoints:
// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/lib/auth";
export const { GET, POST } = handlers;
Protecting pages
Use the auth() function in Server Components:
import { auth } from "@/lib/auth";
import { redirect } from "next/navigation";
export default async function DashboardPage() {
const session = await auth();
if (!session?.user) redirect("/auth/signin");
return
}
Available providers in CheapStack
The SaaS Starter ($49) includes GitHub and Google OAuth.
The SaaS Boilerplate ($59) includes GitHub, Google, Discord, email/password, and magic link authentication.
NextAuth vs alternatives
NextAuth v5 is the most widely used auth library for Next.js. It supports 80+ providers, has built-in database adapters for Drizzle and Prisma, and is maintained by the Next.js team via Auth.js.
Alternatives include Clerk (easier setup, more features, paid at scale), Better Auth (newer, TypeScript-first), and Supabase Auth (tightly integrated with Supabase).
About the author: Muhammad Akbar builds affordable developer tools at CheapStack and writes about bootstrapping products on realistic budgets.