aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/user.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/util/user.ts b/src/util/user.ts
new file mode 100644
index 0000000..f632a11
--- /dev/null
+++ b/src/util/user.ts
@@ -0,0 +1,30 @@
+import type { userData } from "../db/user.ts";
+import db from "../db/user.ts";
+import * as log from "https://deno.land/std/log/mod.ts";
+import type { Context } from "https://deno.land/x/abc@master/mod.ts";
+
+export const getCurrentUser = async (c: Context): Promise<userData | undefined> => {
+ const cookies = getUserCookies(c);
+ try {
+ return await db.getUserByVerificationId(cookies.uid, cookies.verification) as userData;
+ } catch (e) {
+ log.error(e);
+ return undefined;
+ }
+}
+
+export const getUserCookies = (c: Context): userCookies => {
+ const uid = parseInt(c.cookies["uid"]);
+ const verification = c.cookies["verification"];
+ return { uid, verification };
+}
+
+export const isAdmin = async (c: Context): Promise<boolean> => {
+ const user = await getCurrentUser(c);
+ return (user && user.isAdmin) as boolean;
+}
+
+export interface userCookies {
+ uid: number;
+ verification: string;
+}