mirror of
https://github.com/MizuchiLabs/mantrae.git
synced 2025-12-20 13:50:08 -06:00
handle custom backend urls better
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user