Improve App initialization logic with better error handling and logging to prevent infinite loading state
All checks were successful
Build MIPS Binary / build (push) Successful in 4m6s

This commit is contained in:
spinline
2026-02-07 15:45:51 +03:00
parent a948215538
commit 3c13f99652

View File

@@ -25,41 +25,61 @@ pub fn App() -> impl IntoView {
// Check Auth & Setup Status on load
create_effect(move |_| {
spawn_local(async move {
logging::log!("App initialization started...");
// 1. Check Setup Status
logging::log!("Checking setup status...");
let setup_res = gloo_net::http::Request::get("/api/setup/status").send().await;
if let Ok(resp) = setup_res {
if let Ok(status) = resp.json::<SetupStatus>().await {
match setup_res {
Ok(resp) => {
if resp.ok() {
match resp.json::<SetupStatus>().await {
Ok(status) => {
logging::log!("Setup status: completed={}", status.completed);
if !status.completed {
// Redirect to setup if not completed
logging::log!("Setup not completed, redirecting to /setup");
let navigate = use_navigate();
navigate("/setup", Default::default());
set_is_loading.set(false);
return;
}
}
Err(e) => logging::error!("Failed to parse setup status: {}", e),
}
} else {
logging::error!("Setup status request failed: {}", resp.status());
}
}
Err(e) => logging::error!("Network error checking setup status: {}", e),
}
// 2. Check Auth Status
logging::log!("Checking auth status...");
let auth_res = gloo_net::http::Request::get("/api/auth/check").send().await;
if let Ok(resp) = auth_res {
if resp.status() == 200 {
set_is_authenticated.set(true);
// Initialize push notifications logic only if authenticated
// ... (Push notification logic moved here or kept global but guarded)
match auth_res {
Ok(resp) => {
logging::log!("Auth check status: {}", resp.status());
if resp.status() == 200 {
logging::log!("Authenticated!");
set_is_authenticated.set(true);
} else {
logging::log!("Not authenticated, checking if redirect needed");
let navigate = use_navigate();
// If we are already on login or setup, don't redirect loop
let pathname = window().location().pathname().unwrap_or_default();
if pathname != "/login" && pathname != "/setup" {
navigate("/login", Default::default());
}
}
}
Err(e) => logging::error!("Network error checking auth status: {}", e),
}
logging::log!("App initialization finished, disabling loader.");
set_is_loading.set(false);
});
});
// Initialize push notifications after user grants permission (Only if authenticated)
create_effect(move |_| {
if is_authenticated.get() {