geraete/lib/auth.ts
2025-11-24 08:59:14 +01:00

48 lines
983 B
TypeScript

// lib/auth.ts
'use server';
import { getServerSession } from 'next-auth';
import { authOptions } from './auth-options';
import { prisma } from './prisma';
type SessionUser = {
id?: string;
email?: string | null;
name?: string | null;
};
// lib/auth.ts
export async function getCurrentUserId(): Promise<string | null> {
const session = await getServerSession(authOptions);
const user = session?.user as { id?: string; email?: string | null } | undefined;
if (user?.id) {
return user.id;
}
if (user?.email) {
const dbUser = await prisma.user.findUnique({
where: { email: user.email },
select: { nwkennung: true },
});
if (dbUser) return dbUser.nwkennung;
}
return null;
}
export async function getCurrentUser() {
const id = await getCurrentUserId();
if (!id) return null;
return prisma.user.findUnique({
where: { nwkennung: id },
include: {
roles: {
include: { role: true },
},
},
});
}