main
|
Last change
on this file since 3216215 was 8de343e, checked in by Tome <gjorgievtome@…>, 7 months ago |
|
Fix some issues 2
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Rev | Line | |
|---|
| [c30935a] | 1 | import { Abort, getContext } from "telefunc";
|
|---|
| 2 | import * as drizzleQueries from "../../database/drizzle/queries";
|
|---|
| 3 |
|
|---|
| 4 | export function ctx() {
|
|---|
| 5 | return getContext();
|
|---|
| 6 | }
|
|---|
| 7 |
|
|---|
| [6196d60] | 8 | export function parseSessionUserId(sessionUserId: unknown): number | undefined {
|
|---|
| 9 | if (typeof sessionUserId !== "string" && typeof sessionUserId !== "number") return undefined;
|
|---|
| [c30935a] | 10 | const n = Number(sessionUserId);
|
|---|
| [6196d60] | 11 | return Number.isInteger(n) && n > 0 ? n : undefined;
|
|---|
| [c30935a] | 12 | }
|
|---|
| 13 |
|
|---|
| 14 | export function requireUser() {
|
|---|
| 15 | const c = ctx();
|
|---|
| 16 | const userId = parseSessionUserId(c.session?.user?.id);
|
|---|
| 17 | if (!userId) throw Abort();
|
|---|
| 18 | return { c, userId };
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | export function getAuthState() {
|
|---|
| 22 | const c = ctx();
|
|---|
| 23 | const userId = parseSessionUserId(c.session?.user?.id);
|
|---|
| 24 | return {
|
|---|
| 25 | isLoggedIn: Boolean(userId),
|
|---|
| 26 | userId: userId ?? null,
|
|---|
| 27 | username: c.session?.user?.name ?? null,
|
|---|
| [016481f] | 28 | isAdmin: c.session?.user?.isAdmin,
|
|---|
| [c30935a] | 29 | session: c.session,
|
|---|
| 30 | };
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | export async function requireAdmin() {
|
|---|
| 34 | const { c, userId } = requireUser();
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| [8de343e] | 37 | if(!c.session?.user?.isAdmin) throw Abort();
|
|---|
| [898cf68] | 38 |
|
|---|
| 39 | const isAdmin = await drizzleQueries.isAdmin(c.db, userId);
|
|---|
| [c30935a] | 40 | if (!isAdmin) throw Abort();
|
|---|
| [898cf68] | 41 |
|
|---|
| [c30935a] | 42 | return { c, userId };
|
|---|
| 43 | } |
|---|
Note:
See
TracBrowser
for help on using the repository browser.