Fix "validateAccessToken" not returning AccessToken object

This commit is contained in:
Aelita4 2023-12-26 20:58:30 +01:00
parent 1a76f8d5f3
commit 4a34daa54c
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
3 changed files with 5 additions and 13 deletions

View File

@ -1,6 +1,7 @@
import type AccessToken from "../../types/AccessToken";
import { getAccessToken } from "../db/accessTokens";
export default async function validateAccessToken(request: Request): Promise<Response | string> {
export default async function validateAccessToken(request: Request): Promise<Response | AccessToken> {
let accessToken = request.url.split("?")[1]?.split("&").filter((x) => x.split("=")[0] === "token")[0].split("=")[1];
const cookies = request.headers.get("Cookie")?.split(";").map((x) => x.trim().split("=")) ?? [];
@ -41,5 +42,5 @@ export default async function validateAccessToken(request: Request): Promise<Res
}), { status: 403 }
);
return accessToken;
return response;
}

View File

@ -6,20 +6,11 @@ export const GET: APIRoute = async({ request }) => {
const response = await validateAccessToken(request);
if(response instanceof Response) return response;
const accessToken = await getAccessToken(response);
if(!accessToken) return new Response(
JSON.stringify({
code: 404,
message: "Not Found",
data: "Access token not found"
}), { status: 404 }
);
return new Response(
JSON.stringify({
code: 200,
message: "OK",
data: "Access token valid for user " + accessToken.username
data: "Access token valid for user " + response.username
})
);
}

View File

@ -9,7 +9,7 @@ export const POST: APIRoute = async({ request }) => {
const response = await validateAccessToken(request);
if(response instanceof Response) return response;
const user = (await getAccessToken(response))?.username ?? "";
const user = response.username;
const resources = await getUserResources(user);
const buildingId = (await request.json()).building;
const building = buildings.map(cat => cat.buildings.filter(b => b.id === buildingId))[0][0];