Redirect authenticated users away from login/setup pages
Some checks failed
Build MIPS Binary / build (push) Failing after 3m27s
Some checks failed
Build MIPS Binary / build (push) Failing after 3m27s
This commit is contained in:
@@ -56,31 +56,38 @@ pub fn App() -> impl IntoView {
|
|||||||
// 2. Check Auth Status
|
// 2. Check Auth Status
|
||||||
let auth_res = gloo_net::http::Request::get("/api/auth/check").send().await;
|
let auth_res = gloo_net::http::Request::get("/api/auth/check").send().await;
|
||||||
|
|
||||||
match auth_res {
|
match auth_res {
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
if resp.status() == 200 {
|
if resp.status() == 200 {
|
||||||
logging::log!("Authenticated!");
|
logging::log!("Authenticated!");
|
||||||
|
|
||||||
// Parse user info
|
// Parse user info
|
||||||
if let Ok(user_info) = resp.json::<UserResponse>().await {
|
if let Ok(user_info) = resp.json::<UserResponse>().await {
|
||||||
if let Some(store) = use_context::<crate::store::TorrentStore>() {
|
if let Some(store) = use_context::<crate::store::TorrentStore>() {
|
||||||
store.user.set(Some(user_info.username));
|
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);
|
set_is_loading.set(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user