diff options
Diffstat (limited to 'src/login/routes.rs')
| -rw-r--r-- | src/login/routes.rs | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/login/routes.rs b/src/login/routes.rs index 816926e..3c58b10 100644 --- a/src/login/routes.rs +++ b/src/login/routes.rs @@ -8,7 +8,7 @@ use axum::{ use crate::{app::App, clock::RequestedAt, error::InternalError}; -use super::extract::IdentityToken; +use super::{app::LoginError, extract::IdentityToken}; pub fn router() -> Router<App> { Router::new() @@ -28,30 +28,28 @@ async fn on_login( identity: IdentityToken, Form(form): Form<LoginRequest>, ) -> Result<impl IntoResponse, InternalError> { - let token = app.logins().login(&form.name, &form.password, now).await?; - - let resp = if let Some(token) = token { - let identity = identity.set(&token); - (identity, LoginResponse::Successful) - } else { - (identity, LoginResponse::Rejected) - }; - - Ok(resp) + match app.logins().login(&form.name, &form.password, now).await { + Ok(token) => { + let identity = identity.set(&token); + Ok(LoginResponse::Successful(identity)) + } + Err(LoginError::Rejected) => Ok(LoginResponse::Rejected), + Err(other) => Err(other.into()), + } } enum LoginResponse { Rejected, - Successful, + Successful(IdentityToken), } impl IntoResponse for LoginResponse { fn into_response(self) -> Response { match self { + Self::Successful(identity) => (identity, Redirect::to("/")).into_response(), Self::Rejected => { (StatusCode::UNAUTHORIZED, "invalid name or password").into_response() } - Self::Successful => Redirect::to("/").into_response(), } } } |
