From 69243a5590688c8925f7d49acfbc4f4a1c88e7b5 Mon Sep 17 00:00:00 2001 From: spinline Date: Sat, 7 Feb 2026 19:39:53 +0300 Subject: [PATCH] Redirect authenticated users away from login/setup pages --- frontend/src/app.rs | 51 ++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/frontend/src/app.rs b/frontend/src/app.rs index ae68a9e..b4351bf 100644 --- a/frontend/src/app.rs +++ b/frontend/src/app.rs @@ -56,31 +56,38 @@ pub fn App() -> impl IntoView { // 2. Check Auth Status let auth_res = gloo_net::http::Request::get("/api/auth/check").send().await; - match auth_res { - Ok(resp) => { - if resp.status() == 200 { - logging::log!("Authenticated!"); + match auth_res { + Ok(resp) => { + if resp.status() == 200 { + logging::log!("Authenticated!"); - // Parse user info - if let Ok(user_info) = resp.json::().await { - if let Some(store) = use_context::() { - store.user.set(Some(user_info.username)); + // Parse user info + if let Ok(user_info) = resp.json::().await { + if let Some(store) = use_context::() { + store.user.set(Some(user_info.username)); + } + } + + set_is_authenticated.set(true); + + // If user is already authenticated but on login/setup page, redirect to home + let pathname = window().location().pathname().unwrap_or_default(); + if pathname == "/login" || pathname == "/setup" { + logging::log!("Already authenticated, redirecting to home"); + let navigate = use_navigate(); + navigate("/", Default::default()); + } + } else { + logging::log!("Not authenticated, redirecting to /login"); + let navigate = use_navigate(); + 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), } - - set_is_authenticated.set(true); - } else { - logging::log!("Not authenticated, redirecting to /login"); - let navigate = use_navigate(); - 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), - } - set_is_loading.set(false); }); });