fix: redirect settings pages to login if the user is not logged in

This commit is contained in:
Sam 2023-03-30 17:08:53 +02:00
parent ff75075b81
commit 86f272a365
Signed by: sam
GPG key ID: B4EF20DDE721CAA1

View file

@ -6,31 +6,39 @@ import {
type MeUser, type MeUser,
} from "$lib/api/entities"; } from "$lib/api/entities";
import { apiFetchClient } from "$lib/api/fetch"; import { apiFetchClient } from "$lib/api/fetch";
import { redirect } from "@sveltejs/kit";
import type { LayoutLoad } from "./$types"; import type { LayoutLoad } from "./$types";
export const ssr = false; export const ssr = false;
export const load = (async ({ parent }) => { export const load = (async ({ parent }) => {
const user = await apiFetchClient<MeUser>("/users/@me");
const warnings = await apiFetchClient<Warning[]>("/auth/warnings?all=true");
let invites: Invite[] = [];
let invitesEnabled = true;
try { try {
invites = await apiFetchClient<Invite[]>("/auth/invites"); const user = await apiFetchClient<MeUser>("/users/@me");
} catch (e) { const warnings = await apiFetchClient<Warning[]>("/auth/warnings?all=true");
if ((e as APIError).code === ErrorCode.InvitesDisabled) {
invitesEnabled = false; let invites: Invite[] = [];
let invitesEnabled = true;
try {
invites = await apiFetchClient<Invite[]>("/auth/invites");
} catch (e) {
if ((e as APIError).code === ErrorCode.InvitesDisabled) {
invitesEnabled = false;
}
} }
const data = await parent();
return {
...data,
user,
invites,
invitesEnabled,
warnings,
};
} catch (e) {
if ((e as APIError).code !== ErrorCode.InternalServerError) {
throw redirect(303, "/auth/login");
}
throw e;
} }
const data = await parent();
return {
...data,
user,
invites,
invitesEnabled,
warnings,
};
}) satisfies LayoutLoad; }) satisfies LayoutLoad;