Fix "validateAccessToken" not returning AccessToken object
This commit is contained in:
parent
1a76f8d5f3
commit
4a34daa54c
|
@ -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;
|
||||
}
|
|
@ -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
|
||||
})
|
||||
);
|
||||
}
|
|
@ -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];
|
||||
|
|
Loading…
Reference in New Issue