fix profile

This commit is contained in:
not-nullptr 2024-03-10 21:35:50 +00:00
parent 5bfaf053a0
commit a5b383aa5d
2 changed files with 17 additions and 3 deletions

View file

@ -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<T>(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<IJwtData> {
return new Promise((resolve, reject) => {
jwt.verify(token, PUBLIC_KEY, { algorithms: ["RS256"] }, (err, data) => {
if (err) reject(err);
else resolve(data as IJwtData);
});
});
}

View file

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