Return proper error when user authentication cannot be verified

This commit is contained in:
2025-03-18 22:48:17 +01:00
parent d5bb1acbcb
commit 3081757536

View File

@ -3,7 +3,7 @@ use crate::extractors::money_session::MoneySession;
use crate::models::users::User;
use crate::services::users_service;
use actix_web::dev::Payload;
use actix_web::error::ErrorUnauthorized;
use actix_web::error::ErrorPreconditionFailed;
use actix_web::{Error, FromRequest, HttpRequest};
#[derive(Debug, Clone)]
@ -31,7 +31,7 @@ impl FromRequest for AuthExtractor {
if let Some(email) = &AppConfig::get().unsecure_auto_login_email {
let user = users_service::get_user_by_email(email).map_err(|e| {
log::error!("Failed to retrieve dev user: {e}");
ErrorUnauthorized("Unable to retrieve dev user!")
ErrorPreconditionFailed("Unable to retrieve dev user!")
})?;
return Ok(Self {
method: AuthenticatedMethod::Dev,
@ -43,11 +43,11 @@ impl FromRequest for AuthExtractor {
let session = MoneySession::extract(&req).await?;
if let Some(user_id) = session.current_user().map_err(|e| {
log::error!("Failed to retrieve user id: {e}");
ErrorUnauthorized("Failed to read session information!")
ErrorPreconditionFailed("Failed to read session information!")
})? {
let user = users_service::get_user_by_id(user_id).map_err(|e| {
log::error!("Failed to retrieve user from cookie session: {e}");
ErrorUnauthorized("Failed to retrieve user information!")
ErrorPreconditionFailed("Failed to retrieve user information!")
})?;
return Ok(Self {
method: AuthenticatedMethod::Cookie,
@ -55,7 +55,7 @@ impl FromRequest for AuthExtractor {
});
};
Err(ErrorUnauthorized("Authentication required!"))
Err(ErrorPreconditionFailed("Authentication required!"))
})
}
}