WorkshopTasker/server/utils/getRequestingUser.ts

36 lines
839 B
TypeScript
Raw Normal View History

import { getCookie, H3Event } from "h3";
2023-05-24 09:40:45 +02:00
import { database } from "./database";
import SessionToken from "./SessionToken";
import { createError } from "#imports";
2023-05-24 09:40:45 +02:00
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;
});
2023-05-24 09:40:45 +02:00
if (!user) throw createError("User not found");
return user;
}