WorkshopTasker/server/utils/getRequestingUser.ts

26 lines
727 B
TypeScript

import { getCookie, H3Event } from "h3";
import { database, type data } from "./database";
import { type user } from "~/utils/types/database";
import { createError } from "#imports";
export default async function getRequestingUser(e: H3Event) {
const cookie = getCookie(e, "token");
const [[user]] = await database.query(
["SELECT",
"CONVERT(`users`.`id`, CHAR) as `id`,",
"`users`.`username`,",
"`users`.`email`,",
"`users`.`display_name`",
"FROM",
"`sessions`",
"LEFT JOIN `users` ON `sessions`.`user` = `users`.`id`",
"WHERE `sessions`.`id` = ?",
].join(" "),
[cookie],
) as data<user>;
if (!user) throw createError("User not found");
return user;
}