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, }); }