diff options
author | LarsVomMars | 2020-07-30 20:14:56 +0200 |
---|---|---|
committer | LarsVomMars | 2020-07-30 20:14:56 +0200 |
commit | 86c68c3648c94ca9f66f3eb408973368bed28681 (patch) | |
tree | 6d1def94caea53c3de4eb741eb113d7812b000a7 /src/util | |
parent | 14aea28ac22e5d2e9e510c5986daca1bef1c43ae (diff) |
Added user handlers
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/user.ts | 30 |
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; +} |