|
1 | 1 | use crate::controller::ApiResponse; |
| 2 | +use crate::error::{Error as WebError, Result as WebResult}; |
2 | 3 | use axum::{http::StatusCode, response::IntoResponse, Form, Json}; |
3 | 4 | use domain::user::{AuthSession, Credentials}; |
4 | 5 | use log::*; |
@@ -36,17 +37,44 @@ pub struct NextUrl { |
36 | 37 | pub async fn login( |
37 | 38 | mut auth_session: AuthSession, |
38 | 39 | Form(creds): Form<Credentials>, |
39 | | -) -> impl IntoResponse { |
| 40 | +) -> WebResult<impl IntoResponse> { |
40 | 41 | debug!("UserSessionController::login()"); |
41 | 42 |
|
42 | 43 | let user = match auth_session.authenticate(creds.clone()).await { |
43 | 44 | Ok(Some(user)) => user, |
44 | | - Ok(None) => return Err(StatusCode::UNAUTHORIZED.into_response()), |
45 | | - Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR.into_response()), |
| 45 | + Ok(None) => { |
| 46 | + // No user found - this should also be treated as an authentication error |
| 47 | + return Err(WebError::from(domain::error::Error { |
| 48 | + source: None, |
| 49 | + error_kind: domain::error::DomainErrorKind::Internal( |
| 50 | + domain::error::InternalErrorKind::Entity( |
| 51 | + domain::error::EntityErrorKind::Unauthenticated, |
| 52 | + ), |
| 53 | + ), |
| 54 | + })); |
| 55 | + } |
| 56 | + Err(auth_error) => { |
| 57 | + // axum_login errors contain our entity_api::Error in the error field |
| 58 | + warn!("Authentication failed: {:?}", auth_error); |
| 59 | + return Err(WebError::from(domain::error::Error { |
| 60 | + source: Some(Box::new(auth_error)), |
| 61 | + error_kind: domain::error::DomainErrorKind::Internal( |
| 62 | + domain::error::InternalErrorKind::Entity( |
| 63 | + domain::error::EntityErrorKind::Unauthenticated, |
| 64 | + ), |
| 65 | + ), |
| 66 | + })); |
| 67 | + } |
46 | 68 | }; |
47 | 69 |
|
48 | | - if auth_session.login(&user).await.is_err() { |
49 | | - return Err(StatusCode::INTERNAL_SERVER_ERROR.into_response()); |
| 70 | + if let Err(login_error) = auth_session.login(&user).await { |
| 71 | + warn!("Session login failed: {:?}", login_error); |
| 72 | + return Err(WebError::from(domain::error::Error { |
| 73 | + source: Some(Box::new(login_error)), |
| 74 | + error_kind: domain::error::DomainErrorKind::Internal( |
| 75 | + domain::error::InternalErrorKind::Other("Session login failed".to_string()), |
| 76 | + ), |
| 77 | + })); |
50 | 78 | } |
51 | 79 |
|
52 | 80 | let user_session_json = json!({ |
|
0 commit comments