2023-11-06 02:57:00 +01:00
|
|
|
import { getCookie, H3Event } from "h3";
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-11-08 05:35:48 +01:00
|
|
|
import { database } from "./database";
|
2023-11-06 02:57:00 +01:00
|
|
|
|
|
|
|
import { createError } from "#imports";
|
2023-05-24 09:40:45 +02:00
|
|
|
|
|
|
|
export default async function getRequestingUser(e: H3Event) {
|
|
|
|
const cookie = getCookie(e, "token");
|
2023-11-08 05:35:48 +01:00
|
|
|
if (!cookie) throw createError("User not found");
|
|
|
|
const { user } = await database.session.findUnique({
|
|
|
|
where: {
|
|
|
|
id: BigInt(cookie),
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
}
|