42 lines
1.8 KiB
JavaScript
42 lines
1.8 KiB
JavaScript
import { COOKIE_NAME } from "$lib/constants";
|
|
import { getUserFromSession } from "$lib/database";
|
|
import BrickColorsPalette from "$lib/BrickColorsPalette.json";
|
|
|
|
/** @type {import('@sveltejs/kit').Handle} */
|
|
export async function handle({ event, resolve }) {
|
|
if (event.url.pathname !== "/maintenance" && !event.routeId.startsWith("(api)") && process.env.PRODUCTION) {
|
|
return new Response("", { status: 302, headers: { Location: "/maintenance" } });
|
|
}
|
|
|
|
const cookie = event.cookies.get(COOKIE_NAME);
|
|
if (!cookie) {
|
|
if (event.routeId.startsWith("(app)")) return new Response("", { status: 302, headers: { Location: "/landing" } });
|
|
return await resolve(event);
|
|
}
|
|
|
|
let user = await getUserFromSession(cookie, event.request.headers.get("x-forwarded-for") || event.getClientAddress());
|
|
if (!user) {
|
|
event.cookies.delete(COOKIE_NAME, { secure: !!process.env.PRODUCTION });
|
|
if (event.routeId.startsWith("(app)")) return new Response("", { status: 302, headers: { Location: "/landing" } });
|
|
} else
|
|
event.locals.user = {
|
|
_id: user._id,
|
|
username: user.username,
|
|
currency: user.currency,
|
|
thumbnails: {
|
|
headshot: "/img/headshot.png",
|
|
bodyshot: "/img/derpecated.png"
|
|
},
|
|
bodyColors: {
|
|
HeadColor: BrickColorsPalette[Math.floor(Math.random() * BrickColorsPalette.length)],
|
|
TorsoColor: BrickColorsPalette[Math.floor(Math.random() * BrickColorsPalette.length)],
|
|
LeftArmColor: BrickColorsPalette[Math.floor(Math.random() * BrickColorsPalette.length)],
|
|
RightArmColor: BrickColorsPalette[Math.floor(Math.random() * BrickColorsPalette.length)],
|
|
LeftLegColor: BrickColorsPalette[Math.floor(Math.random() * BrickColorsPalette.length)],
|
|
RightLegColor: BrickColorsPalette[Math.floor(Math.random() * BrickColorsPalette.length)]
|
|
}
|
|
};
|
|
|
|
return await resolve(event);
|
|
}
|