diff --git a/src/api/auth.rs b/src/api/auth.rs index 6fe913a..956b040 100644 --- a/src/api/auth.rs +++ b/src/api/auth.rs @@ -190,7 +190,7 @@ pub async fn login(username: String, password: String) -> Result Result NOW()", &[&user_id], @@ -253,27 +260,27 @@ pub async fn login(username: String, password: String) -> Result= max_sessions { - client - .execute( - "DELETE FROM sessions WHERE id IN ( - SELECT id FROM sessions - WHERE user_id = $1 AND expires_at > NOW() - ORDER BY created_at ASC - LIMIT 1 - )", - &[&user_id], - ) - .await - .map_err(AppError::query)?; - } - - client - .execute( - "INSERT INTO sessions (user_id, token_hash, user_agent, expires_at) VALUES ($1, $2, $3, $4)", - &[&user_id, &token_hash, &None::, &expires_at], + tx.execute( + "DELETE FROM sessions WHERE id IN ( + SELECT id FROM sessions + WHERE user_id = $1 AND expires_at > NOW() + ORDER BY created_at ASC + LIMIT 1 + )", + &[&user_id], ) .await .map_err(AppError::query)?; + } + + tx.execute( + "INSERT INTO sessions (user_id, token_hash, user_agent, expires_at) VALUES ($1, $2, $3, $4)", + &[&user_id, &token_hash, &None::, &expires_at], + ) + .await + .map_err(AppError::query)?; + + tx.commit().await.map_err(AppError::query)?; let cookie = session::session_cookie(&token, 30 * 24 * 60 * 60, session::cookie_secure()); // 通过 Dioxus FullstackContext 设置 HttpOnly Cookie 响应头。