update nextauth

This commit is contained in:
Elias Bennour 2025-05-25 16:05:44 +02:00
parent 2d1e1ea88d
commit 1e9b2c702b

View File

@ -1,13 +1,14 @@
// Datei: pages/api/auth/[...nextauth].ts
import NextAuth, {AuthOptions, TokenSet} from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import CredentialsProvider, {CredentialInput} from "next-auth/providers/credentials";
import KeycloakProvider, { KeycloakProfile } from "next-auth/providers/keycloak";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import prisma from "../../../lib/prisma";
import bcrypt from "bcryptjs";
import type { User, Account } from "next-auth"; // Account importieren für den signIn Callback
import type { User, Account, Profile } from "next-auth";
import {AdapterUser} from "next-auth/adapters"; // Account und Profile importieren für den signIn Callback
export const authOptions: AuthOptions = {
adapter: PrismaAdapter(prisma),
@ -89,16 +90,21 @@ export const authOptions: AuthOptions = {
}),
],
callbacks: {
// NEU: signIn Callback, um Account-Daten vor dem Speichern anzupassen
async signIn({account}) {
async signIn({account}: { user: User | AdapterUser, account: Account | null, profile?: Profile | KeycloakProfile, email?: { verificationRequest?: boolean }, credentials?: Record<string, CredentialInput> }) {
if (account && account.provider === "keycloak") {
// Prüfe, ob 'not-before-policy' im Account-Objekt von Keycloak vorhanden ist
// und benenne es in 'not_before_policy' um, damit es zum Prisma-Schema passt.
// Das account-Objekt hier enthält die rohen Daten vom Token-Endpunkt.
const keycloakAccount = account as Account & { 'not-before-policy'?: any, 'not_before_policy'?: any };
// Das 'account'-Objekt hier enthält die rohen Daten vom Token-Endpunkt des Providers.
// Wir verwenden eine Typ-Assertion, um TypeScript mitzuteilen, dass wir zusätzliche,
// provider-spezifische Felder erwarten könnten.
const keycloakAccount = account as Account & {
'not-before-policy'?: unknown; // Geändert von any zu unknown
not_before_policy?: unknown; // Geändert von any zu unknown
// Füge hier weitere provider-spezifische Felder hinzu, falls nötig
};
if (keycloakAccount['not-before-policy'] !== undefined) {
console.log(`[NextAuth.js] signIn callback: Renaming 'not-before-policy' to 'not_before_policy' for account: ${account.providerAccountId}`);
// Der Wert wird einfach kopiert. Die Typüberprüfung für den Wert selbst ist hier weniger kritisch
// als die Vermeidung von 'any' in der Typdefinition.
keycloakAccount.not_before_policy = keycloakAccount['not-before-policy'];
delete keycloakAccount['not-before-policy'];
}