A database hiccup is not a sign-out
Browse files at this commita672cd417f8895647220afa798e31330fa80e1aeParent: 659e9a2- fix
- a database hiccup is not a sign-out
3 changed files +44 -14
- crates/izlek-web/src/auth.rs +4 -1
- crates/izlek-web/src/files.rs +19 -8
- crates/izlek-web/src/server.rs +21 -5
crates/izlek-web/src/auth.rsmodified+4 -1
| @@ -338,7 +338,10 @@ pub const WORKSPACE_NAME: &str = "Izlek"; | ||
| 338 | 338 | pub async fn current_gate() -> Result<Gate, ServerFnError> { |
| 339 | 339 | use crate::server::{accounts, current_user}; |
| 340 | 340 | |
| 341 | - if let Some(user) = current_user().await { | |
| 341 | + if let Some(user) = current_user() | |
| 342 | + .await | |
| 343 | + .map_err(|e| ServerFnError::new(e.to_string()))? | |
| 344 | + { | |
| 342 | 345 | return Ok(Gate::SignedIn(Me { |
| 343 | 346 | id: user.id, |
| 344 | 347 | display_name: user.display_name, |
crates/izlek-web/src/files.rsmodified+19 -8
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | use axum::extract::{Extension, Multipart, Path}; |
| 10 | 10 | use axum::http::{HeaderMap, HeaderValue, StatusCode}; |
| 11 | 11 | use axum::response::{IntoResponse, Redirect, Response}; |
| 12 | -use izlek_core::accounts::Accounts; | |
| 12 | +use izlek_core::accounts::{AccountError, Accounts}; | |
| 13 | 13 | use izlek_core::store::{NewAttachment, User}; |
| 14 | 14 | use time::OffsetDateTime; |
| 15 | 15 | |
| @@ -18,9 +18,16 @@ use crate::detail::guard; | ||
| 18 | 18 | |
| 19 | 19 | /// The person behind this request, read off the session cookie in `headers` |
| 20 | 20 | /// rather than the leptos request context a plain axum handler has none of. |
| 21 | -pub async fn user_of(accounts: &Accounts, headers: &HeaderMap) -> Option<User> { | |
| 22 | - let session = crate::server::session_in(headers)?; | |
| 23 | - accounts.authenticate(&session).await.ok().flatten() | |
| 21 | +/// | |
| 22 | +/// A store error is returned as such, not folded into "nobody" — the callers | |
| 23 | +/// below tell it apart from a missing session the same way `server::require_user` | |
| 24 | +/// does, so a database hiccup answers "something went wrong" rather than | |
| 25 | +/// sending a signed-in person back through the door. | |
| 26 | +pub async fn user_of(accounts: &Accounts, headers: &HeaderMap) -> Result<Option<User>, AccountError> { | |
| 27 | + let Some(session) = crate::server::session_in(headers) else { | |
| 28 | + return Ok(None); | |
| 29 | + }; | |
| 30 | + accounts.authenticate(&session).await | |
| 24 | 31 | } |
| 25 | 32 | |
| 26 | 33 | /// Where the browser lands once the upload is settled, success or refusal |
| @@ -89,8 +96,10 @@ pub async fn upload( | ||
| 89 | 96 | headers: HeaderMap, |
| 90 | 97 | mut multipart: Multipart, |
| 91 | 98 | ) -> Response { |
| 92 | - let Some(user) = user_of(&accounts, &headers).await else { | |
| 93 | - return home(Refusal::SignInFirst); | |
| 99 | + let user = match user_of(&accounts, &headers).await { | |
| 100 | + Ok(Some(user)) => user, | |
| 101 | + Ok(None) => return home(Refusal::SignInFirst), | |
| 102 | + Err(error) => return home(error.into()), | |
| 94 | 103 | }; |
| 95 | 104 | if !user.role.can_comment() { |
| 96 | 105 | return home(Refusal::Forbidden); |
| @@ -239,8 +248,10 @@ pub async fn download( | ||
| 239 | 248 | headers: HeaderMap, |
| 240 | 249 | Path(id): Path<String>, |
| 241 | 250 | ) -> Response { |
| 242 | - let Some(user) = user_of(&accounts, &headers).await else { | |
| 243 | - return home(Refusal::SignInFirst); | |
| 251 | + let user = match user_of(&accounts, &headers).await { | |
| 252 | + Ok(Some(user)) => user, | |
| 253 | + Ok(None) => return home(Refusal::SignInFirst), | |
| 254 | + Err(error) => return home(error.into()), | |
| 244 | 255 | }; |
| 245 | 256 | |
| 246 | 257 | let store = accounts.store().clone(); |
crates/izlek-web/src/server.rsmodified+21 -5
| @@ -197,15 +197,31 @@ fn write_cookie(value: &str) { | ||
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | -/// The person behind this request, or nobody. | |
| 201 | -pub async fn current_user() -> Option<User> { | |
| 202 | - let presented = presented_session()?; | |
| 203 | - accounts().authenticate(&presented).await.ok().flatten() | |
| 200 | +/// The person behind this request, or nobody, or the store failing to say. | |
| 201 | +/// | |
| 202 | +/// A store error is not "nobody" — a busy database mid-drag is a fact about | |
| 203 | +/// the database, not about whether this browser is signed in, and a caller | |
| 204 | +/// that folded the two together would send a signed-in person to a sign-in | |
| 205 | +/// screen because a write elsewhere held a lock for a moment. | |
| 206 | +pub async fn current_user() -> Result<Option<User>, AccountError> { | |
| 207 | + let Some(presented) = presented_session() else { | |
| 208 | + return Ok(None); | |
| 209 | + }; | |
| 210 | + accounts().authenticate(&presented).await | |
| 204 | 211 | } |
| 205 | 212 | |
| 206 | 213 | /// The person behind this request, or a refusal the caller can return as-is. |
| 214 | +/// | |
| 215 | +/// A store error becomes `Refusal::Unavailable` — the same place every other | |
| 216 | +/// account error already lands, via `From<AccountError> for Refusal` below — | |
| 217 | +/// rather than `SignInFirst`, which would tell a signed-in person to sign in | |
| 218 | +/// again over what was only a database hiccup. | |
| 207 | 219 | pub async fn require_user() -> Result<User, Refusal> { |
| 208 | - current_user().await.ok_or(Refusal::SignInFirst) | |
| 220 | + match current_user().await { | |
| 221 | + Ok(Some(user)) => Ok(user), | |
| 222 | + Ok(None) => Err(Refusal::SignInFirst), | |
| 223 | + Err(error) => Err(error.into()), | |
| 224 | + } | |
| 209 | 225 | } |
| 210 | 226 | |
| 211 | 227 | /// The admin behind this request. A member or a viewer is refused *here*, not |