mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 10:51:36 +01:00
Initial commit
This commit is contained in:
28
web-app/src/hooks.server.ts
Normal file
28
web-app/src/hooks.server.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
|
||||
export const handle = async ({ event, resolve }) => {
|
||||
const userData = await event.fetch("http://localhost:3000/user", {
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user