import { getCookie, H3Event } from "h3"; import { database } from "./database"; import SessionToken from "./SessionToken"; import { createError } from "#imports"; export default async function getRequestingUser(e: H3Event) { const cookie = getCookie(e, "token"); if (!cookie) throw createError("User not found"); const { user } = await database.session.findUnique({ where: { ...SessionToken.fromString(cookie).toPrisma(), expiry_date: { gte: new Date(), }, }, select: { user: { select: { display_name: true, email: true, id: true, username: true, }, }, }, }).then((e) => { if (e === null) throw createError("User not found"); return e; }); if (!user) throw createError("User not found"); return user; }