From 401556c24e769d83fd0ce2406223b81c453a779d Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 26 May 2026 08:21:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20login.rs=20=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=8F=98=E9=87=8F=E5=90=8D=E9=94=99=E8=AF=AF=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=BB=98=E8=AE=A4=20features?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Cargo.toml: 默认启用 web 和 server features - login.rs: 修复 cookie 设置中的变量名错误 (token -> _token) - 格式化 login.rs 中的结构体模式匹配 Co-Authored-By: Claude Opus 4.7 (1M context) --- Cargo.toml | 2 +- src/pages/login.rs | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6d8d94e..9d6074e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ web-sys = { version = "0.3", features = ["Document", "Window", "HtmlDocument", " wasm-bindgen = "0.2" [features] -default = [] +default = ["web", "server"] web = ["dioxus/web"] server = [ "dioxus/server", diff --git a/src/pages/login.rs b/src/pages/login.rs index c4a6a57..d6965f6 100644 --- a/src/pages/login.rs +++ b/src/pages/login.rs @@ -19,18 +19,23 @@ pub fn LoginPage() -> Element { spawn(async move { match login(username_val, password_val).await { - Ok(AuthResponse { success: true, token: Some(_token), .. }) => { + Ok(AuthResponse { + success: true, + token: Some(_token), + .. + }) => { // 设置 cookie (client-side, not HttpOnly but works for now) #[cfg(target_arch = "wasm32")] { let cookie = format!( "session={}; path=/; max-age={}; SameSite=Lax", - token, + _token, 30 * 24 * 60 * 60 // 30 days ); if let Some(window) = web_sys::window() { if let Some(document) = window.document() { - let _ = document.dyn_into::() + let _ = document + .dyn_into::() .map(|d| d.set_cookie(&cookie)); } } @@ -38,10 +43,18 @@ pub fn LoginPage() -> Element { // 跳转到 admin 页面 let _ = dioxus::router::navigator().push("/admin"); } - Ok(AuthResponse { success: false, message, .. }) => { + Ok(AuthResponse { + success: false, + message, + .. + }) => { error.set(Some(message)); } - Ok(AuthResponse { success: true, token: None, .. }) => { + Ok(AuthResponse { + success: true, + token: None, + .. + }) => { error.set(Some("登录异常".to_string())); } Err(e) => {