← Back to Documentation
Code Examples
Production-ready code snippets and implementation patterns used across all platforms.
NextAuth.js Setup
Configure authentication with multiple providers
// app/api/auth/[...nextauth]/route.ts
import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google"
import { DrizzleAdapter } from "@auth/drizzle-adapter"
import { db } from "@/lib/db"
export const authOptions = {
adapter: DrizzleAdapter(db),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
],
callbacks: {
session: async ({ session, user }) => {
session.user.id = user.id
return session
},
},
}
const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }Protected Route Example
Server-side authentication check
// app/dashboard/page.tsx
import { getServerSession } from "next-auth"
import { redirect } from "next/navigation"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
export default async function DashboardPage() {
const session = await getServerSession(authOptions)
if (!session) {
redirect("/api/auth/signin")
}
return (
<div>
<h1>Welcome, {session.user.name}!</h1>
</div>
)
}