diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index 43d9cf19..2b349306 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -195,6 +195,31 @@ export const authOptions: NextAuthOptions = { throw new Error("RefreshAccessTokenError"); } + try { + console.log('Creating/updating user in session callback:', { + id: token.sub, + email: token.email + }); + + // Create or update user in local database + await prisma.user.upsert({ + where: { id: token.sub as string }, + update: { + email: token.email as string, + password: '', // We don't store password for Keycloak users + }, + create: { + id: token.sub as string, + email: token.email as string, + password: '', // We don't store password for Keycloak users + }, + }); + + console.log('User created/updated successfully'); + } catch (error) { + console.error('Error creating/updating user in session callback:', error); + } + session.accessToken = token.accessToken; session.user = { id: token.sub as string,