// rocie - An enterprise grocery management system - Web app // // Copyright (C) 2026 Benedikt Peetz // SPDX-License-Identifier: GPL-3.0-or-later // // This file is part of Rocie. // // You should have received a copy of the License along with this program. // If not, see . use leptos::{ IntoView, component, prelude::{Get, Show, signal}, task::spawn_local, view, }; use leptos_router::{ NavigateOptions, hooks::{use_navigate, use_query_map}, }; use rocie_client::models::LoginInfo; use rocie_macros::Form; use crate::{ api::{get_config, login_external_wrapped}, components::{banner::Banner, catch_errors::CatchErrors, site_header::SiteHeader}, }; #[component] pub fn Login() -> impl IntoView { let back = || { let back = use_query_map() .get() .get("back") .expect("Should always have a back, because the router would otherwise not match"); if back.starts_with('/') { back } else { // Prevent a redirect like `/login?back=https://gnu.org` to work "/".to_owned() } }; let (error_message, error_message_set) = signal(None); view! { { Form! { on_submit = |user_name, password| { let config = get_config!(); let navigate = use_navigate(); let back = back(); spawn_local(async move { match login_external_wrapped( &config, LoginInfo { user_name, password } ).await { Ok(()) => { navigate(back.as_str(), NavigateOptions::default()); } Err(err) => error_message_set.set(Some(format!("Failed to login: {err}"))), } }); }; } } } }