auth.ts
1,481 bytes
| 1 | /** |
|---|---|
| 2 | * Auth.js (NextAuth v5) configuration - GitHub only. |
| 3 | * |
| 4 | * This is a personal/portfolio tool, not a SaaS, so auth is intentionally |
| 5 | * minimal: one OAuth provider, database sessions via the Prisma adapter, and a |
| 6 | * graceful no-op when GitHub credentials aren't configured (the public demo |
| 7 | * runs entirely unauthenticated). |
| 8 | */ |
| 9 | import { PrismaAdapter } from '@auth/prisma-adapter'; |
| 10 | import NextAuth from 'next-auth'; |
| 11 | import GitHub from 'next-auth/providers/github'; |
| 12 | import { prisma } from '@/db/client'; |
| 13 | import { env, isGithubAuthConfigured } from '@/lib/env'; |
| 14 | |
| 15 | export const { handlers, auth, signIn, signOut } = NextAuth({ |
| 16 | adapter: PrismaAdapter(prisma), |
| 17 | session: { strategy: 'database' }, |
| 18 | trustHost: env.AUTH_TRUST_HOST, |
| 19 | // Fall back to the (always-present) encryption key so demo deployments don't |
| 20 | // need a separate AUTH_SECRET just to render pages without noisy warnings. |
| 21 | secret: env.AUTH_SECRET ?? env.ENCRYPTION_KEY, |
| 22 | providers: isGithubAuthConfigured |
| 23 | ? [ |
| 24 | GitHub({ |
| 25 | clientId: env.AUTH_GITHUB_ID!, |
| 26 | clientSecret: env.AUTH_GITHUB_SECRET!, |
| 27 | }), |
| 28 | ] |
| 29 | : [], |
| 30 | pages: { |
| 31 | signIn: '/signin', |
| 32 | }, |
| 33 | callbacks: { |
| 34 | session({ session, user }) { |
| 35 | if (session.user) session.user.id = user.id; |
| 36 | return session; |
| 37 | }, |
| 38 | }, |
| 39 | }); |
| 40 | |
| 41 | /** Convenience: the current user id, or null. */ |
| 42 | export async function currentUserId(): Promise<string | null> { |
| 43 | const session = await auth(); |
| 44 | return session?.user?.id ?? null; |
| 45 | } |
| 46 | |