trepa-web/src/pages/api/accounts/login.ts
2024-07-01 21:58:06 +02:00

28 lines
670 B
TypeScript

import type { APIRoute } from "astro";
export const POST: APIRoute = async ({ request, cookies }) => {
const reqBody = await request.text();
if (!reqBody) throw new Error("body not found");
const res = await fetch(`${import.meta.env.API_URL}/accounts/login`, {
method: "POST",
body: reqBody,
headers: {
"Content-Type": "application/json",
},
});
const resBody = await res.text();
const resBodyJson = JSON.parse(resBody);
if (res.status === 201 && resBodyJson?.jwt) {
cookies.set("jwt", resBodyJson.jwt, {
secure: true,
httpOnly: true,
path: "/",
sameSite: "strict",
});
}
return new Response(resBody, { status: res.status });
};