From a672cd417f8895647220afa798e31330fa80e1ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Aug 2026 23:44:11 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(auth):=20a=20database=20hicc?= =?UTF-8?q?up=20is=20not=20a=20sign-out?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit current_user swallowed authenticate's store error into None, so a locked-database read during a drag's write burst turned into Refusal::SignInFirst for a genuinely signed-in person. current_user and files::user_of now return the store's Result instead of folding it away; require_user maps a store error onto the existing Refusal::Unavailable path (via From for Refusal) rather than SignInFirst, and current_gate propagates it as a ServerFnError. --- crates/izlek-web/src/auth.rs | 5 ++++- crates/izlek-web/src/files.rs | 27 +++++++++++++++++++-------- crates/izlek-web/src/server.rs | 26 +++++++++++++++++++++----- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/crates/izlek-web/src/auth.rs b/crates/izlek-web/src/auth.rs index 74cdb72..bbe7f9a 100644 --- a/crates/izlek-web/src/auth.rs +++ b/crates/izlek-web/src/auth.rs @@ -338,7 +338,10 @@ pub const WORKSPACE_NAME: &str = "Izlek"; pub async fn current_gate() -> Result { use crate::server::{accounts, current_user}; - if let Some(user) = current_user().await { + if let Some(user) = current_user() + .await + .map_err(|e| ServerFnError::new(e.to_string()))? + { return Ok(Gate::SignedIn(Me { id: user.id, display_name: user.display_name, diff --git a/crates/izlek-web/src/files.rs b/crates/izlek-web/src/files.rs index e6c84d2..e721308 100644 --- a/crates/izlek-web/src/files.rs +++ b/crates/izlek-web/src/files.rs @@ -9,7 +9,7 @@ use axum::extract::{Extension, Multipart, Path}; use axum::http::{HeaderMap, HeaderValue, StatusCode}; use axum::response::{IntoResponse, Redirect, Response}; -use izlek_core::accounts::Accounts; +use izlek_core::accounts::{AccountError, Accounts}; use izlek_core::store::{NewAttachment, User}; use time::OffsetDateTime; @@ -18,9 +18,16 @@ use crate::detail::guard; /// The person behind this request, read off the session cookie in `headers` /// rather than the leptos request context a plain axum handler has none of. -pub async fn user_of(accounts: &Accounts, headers: &HeaderMap) -> Option { - let session = crate::server::session_in(headers)?; - accounts.authenticate(&session).await.ok().flatten() +/// +/// A store error is returned as such, not folded into "nobody" — the callers +/// below tell it apart from a missing session the same way `server::require_user` +/// does, so a database hiccup answers "something went wrong" rather than +/// sending a signed-in person back through the door. +pub async fn user_of(accounts: &Accounts, headers: &HeaderMap) -> Result, AccountError> { + let Some(session) = crate::server::session_in(headers) else { + return Ok(None); + }; + accounts.authenticate(&session).await } /// Where the browser lands once the upload is settled, success or refusal @@ -89,8 +96,10 @@ pub async fn upload( headers: HeaderMap, mut multipart: Multipart, ) -> Response { - let Some(user) = user_of(&accounts, &headers).await else { - return home(Refusal::SignInFirst); + let user = match user_of(&accounts, &headers).await { + Ok(Some(user)) => user, + Ok(None) => return home(Refusal::SignInFirst), + Err(error) => return home(error.into()), }; if !user.role.can_comment() { return home(Refusal::Forbidden); @@ -239,8 +248,10 @@ pub async fn download( headers: HeaderMap, Path(id): Path, ) -> Response { - let Some(user) = user_of(&accounts, &headers).await else { - return home(Refusal::SignInFirst); + let user = match user_of(&accounts, &headers).await { + Ok(Some(user)) => user, + Ok(None) => return home(Refusal::SignInFirst), + Err(error) => return home(error.into()), }; let store = accounts.store().clone(); diff --git a/crates/izlek-web/src/server.rs b/crates/izlek-web/src/server.rs index 1139740..5a1dc0c 100644 --- a/crates/izlek-web/src/server.rs +++ b/crates/izlek-web/src/server.rs @@ -197,15 +197,31 @@ fn write_cookie(value: &str) { } } -/// The person behind this request, or nobody. -pub async fn current_user() -> Option { - let presented = presented_session()?; - accounts().authenticate(&presented).await.ok().flatten() +/// The person behind this request, or nobody, or the store failing to say. +/// +/// A store error is not "nobody" — a busy database mid-drag is a fact about +/// the database, not about whether this browser is signed in, and a caller +/// that folded the two together would send a signed-in person to a sign-in +/// screen because a write elsewhere held a lock for a moment. +pub async fn current_user() -> Result, AccountError> { + let Some(presented) = presented_session() else { + return Ok(None); + }; + accounts().authenticate(&presented).await } /// The person behind this request, or a refusal the caller can return as-is. +/// +/// A store error becomes `Refusal::Unavailable` — the same place every other +/// account error already lands, via `From for Refusal` below — +/// rather than `SignInFirst`, which would tell a signed-in person to sign in +/// again over what was only a database hiccup. pub async fn require_user() -> Result { - current_user().await.ok_or(Refusal::SignInFirst) + match current_user().await { + Ok(Some(user)) => Ok(user), + Ok(None) => Err(Refusal::SignInFirst), + Err(error) => Err(error.into()), + } } /// The admin behind this request. A member or a viewer is refused *here*, not