mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 10:51:36 +01:00
32 lines
869 B
TypeScript
32 lines
869 B
TypeScript
import { redirect } from "@sveltejs/kit";
|
|
import { API_BASE_PREFIX } from "$lib/server/utils";
|
|
|
|
export const handle = async ({ event, resolve }) => {
|
|
if (!event.url.pathname.startsWith("/api/")) {
|
|
const userData = await event.fetch(`${API_BASE_PREFIX}/account`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${event.cookies.get("session_token")}`,
|
|
Accept: "application/vnd.pgrst.object+json"
|
|
}
|
|
});
|
|
|
|
if (!userData.ok && !["/login", "/register"].includes(event.url.pathname)) {
|
|
throw redirect(303, "/login");
|
|
}
|
|
|
|
if (userData.ok) {
|
|
if (["/login", "/register"].includes(event.url.pathname)) {
|
|
throw redirect(303, "/");
|
|
}
|
|
|
|
const user = await userData.json();
|
|
|
|
event.locals.user = user;
|
|
}
|
|
}
|
|
|
|
return await resolve(event);
|
|
};
|