WorkshopTasker/server/utils/getRequestingUser.ts
2023-05-24 09:40:45 +02:00

25 lines
701 B
TypeScript

/* global getCookie, createError */
import { H3Event } from "h3";
import { database, data } from "./database";
import { user } from "~/utils/types/database";
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;
}