Files
Kaname/src/auth.ts
T
Pascal LinxweilerandClaude Opus 5.5 891e496157 Milestone (a): project skeleton and OIDC login
- Next.js 16 + Tailwind + shadcn/ui skeleton
- Auth.js v5 generic OIDC (Pocket ID, PKCE) with optional group gate
- proxy.ts gates all pages and API routes
- Drizzle schema (models, files, jobs, logs, worker state) on SQLite/libsql
- Worker skeleton: migrations, interrupted-job recovery, heartbeat

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 15:32:52 +02:00

65 lines
2.0 KiB
TypeScript

import NextAuth, { type NextAuthConfig } from "next-auth";
import { authEnv } from "@/lib/server-env";
declare module "next-auth" {
interface Session {
user: {
name?: string | null;
email?: string | null;
image?: string | null;
groups: string[];
};
}
}
export const OIDC_PROVIDER_ID = "pocket-id";
function groupsFromProfile(profile: unknown): string[] {
const groups = (profile as { groups?: unknown } | undefined)?.groups;
if (Array.isArray(groups)) return groups.filter((g): g is string => typeof g === "string");
if (typeof groups === "string") return [groups];
return [];
}
// Config is built lazily (per request) so `next build` doesn't need OIDC env vars.
export const { handlers, auth, signIn, signOut } = NextAuth((): NextAuthConfig => {
const env = authEnv();
const restrictGroups = env.OIDC_ALLOWED_GROUPS.length > 0;
return {
trustHost: true,
session: { strategy: "jwt", maxAge: 7 * 24 * 60 * 60 },
pages: { signIn: "/login", error: "/login" },
providers: [
{
id: OIDC_PROVIDER_ID,
name: "Pocket ID",
type: "oidc",
issuer: env.OIDC_ISSUER,
clientId: env.OIDC_CLIENT_ID,
clientSecret: env.OIDC_CLIENT_SECRET,
checks: ["pkce", "state", "nonce"],
authorization: {
params: { scope: restrictGroups ? "openid profile email groups" : "openid profile email" },
},
},
],
callbacks: {
// Group gate. Fails closed: if groups are required and the claim is missing, deny.
signIn({ profile }) {
if (!restrictGroups) return true;
const groups = groupsFromProfile(profile);
return groups.some((g) => env.OIDC_ALLOWED_GROUPS.includes(g));
},
jwt({ token, profile }) {
if (profile) token.groups = groupsFromProfile(profile);
return token;
},
session({ session, token }) {
session.user.groups = Array.isArray(token.groups) ? (token.groups as string[]) : [];
return session;
},
},
};
});