handle custom backend urls better

This commit is contained in:
d34dscene
2025-07-02 03:07:50 +02:00
parent 9a5de36278
commit 2b4ce97b26
7 changed files with 375 additions and 210 deletions

View File

@@ -1,6 +1,6 @@
import type { LayoutLoad } from "./$types";
import { goto } from "$app/navigation";
import { useClient } from "$lib/api";
import { checkHealth, useClient } from "$lib/api";
import { profile } from "$lib/stores/profile";
import { user } from "$lib/stores/user";
import { UserService } from "$lib/gen/mantrae/v1/user_pb";
@@ -12,23 +12,41 @@ export const trailingSlash = "always";
export const load: LayoutLoad = async ({ url, fetch }) => {
const currentPath = url.pathname;
const isPublic = currentPath.startsWith("/login");
const isPublic =
currentPath.startsWith("/login") || currentPath.startsWith("/welcome");
try {
const userClient = useClient(UserService, fetch);
const resUser = await userClient.getUser({});
if (!resUser.user) throw new Error("Authentication failed");
user.value = resUser.user;
if (!profile.id) {
const profileClient = useClient(ProfileService, fetch);
const resProfile = await profileClient.listProfiles({});
profile.value = resProfile.profiles[0];
const healthy = await checkHealth();
if (!healthy) {
// No backend, force redirect to welcome screen to enter backend URL
if (currentPath !== "/welcome") {
await goto("/welcome");
user.clear();
return {}; // stop loading other data
}
} else {
// Backend reachable
if (currentPath === "/welcome") {
// Backend is back, redirect from welcome to login
await goto("/login");
return {};
}
if (isPublic) await goto("/");
} catch (_) {
user.clear();
if (!isPublic) await goto("/login");
try {
const userClient = useClient(UserService, fetch);
const resUser = await userClient.getUser({});
if (!resUser.user) throw new Error("Authentication failed");
user.value = resUser.user;
if (!profile.id) {
const profileClient = useClient(ProfileService, fetch);
const resProfile = await profileClient.listProfiles({});
profile.value = resProfile.profiles[0];
}
if (isPublic) await goto("/");
} catch (_) {
user.clear();
if (!isPublic) await goto("/login");
}
}
};