From a5b383aa5de01f1ffbe9c51c884aa3a2c6a659c3 Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Sun, 10 Mar 2024 21:35:50 +0000 Subject: [PATCH] fix profile --- src/lib/server/util/index.ts | 12 ++++++++++++ src/routes/profile/+server.ts | 8 +++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/lib/server/util/index.ts b/src/lib/server/util/index.ts index 439e7b7..3bca8e0 100644 --- a/src/lib/server/util/index.ts +++ b/src/lib/server/util/index.ts @@ -1,4 +1,7 @@ +import type { IJwtData } from "$types/auth"; import type { Role } from "$types/db"; +import { PUBLIC_KEY } from "../secrets"; +import jwt from "jsonwebtoken"; export function json(body: T): Response { return new Response(JSON.stringify(body), { @@ -15,3 +18,12 @@ export function serializeRoles(roles: Role[]): string { export function deserializeRoles(roles: string): Role[] { return roles.split("|") as Role[]; } + +export async function getJwtData(token: string): Promise { + return new Promise((resolve, reject) => { + jwt.verify(token, PUBLIC_KEY, { algorithms: ["RS256"] }, (err, data) => { + if (err) reject(err); + else resolve(data as IJwtData); + }); + }); +} diff --git a/src/routes/profile/+server.ts b/src/routes/profile/+server.ts index c968a37..8d73e29 100644 --- a/src/routes/profile/+server.ts +++ b/src/routes/profile/+server.ts @@ -1,8 +1,10 @@ import { json } from "$lib/server/util/index"; +import { useAuth } from "$lib/util/api/index.js"; -export function GET({ request }) { - console.log(request.headers.get("Authorization")); +export async function GET({ request }) { + const user = await useAuth(request.headers.get("authorization") || ""); + if (!user) return new Response(null, { status: 401 }); return json({ - username: "nullptr", + username: user.username, }); }