From c7500ae6a32b9f8107604f771d7051c3566884ee Mon Sep 17 00:00:00 2001 From: azdle Date: Thu, 24 Jul 2025 15:04:11 -0500 Subject: [PATCH] use dedicated error page I can't re-display the form as I'd like to becuase I don't have access to the CSRF token when rendering the page from the error hander. --- src/server/routes/auth/mod.rs | 46 ++++++++++++++++++++++++++--------- src/server/routes/mod.rs | 6 +++++ templates/error.html | 5 ++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 templates/error.html diff --git a/src/server/routes/auth/mod.rs b/src/server/routes/auth/mod.rs index 0b695af..1aa53dc 100644 --- a/src/server/routes/auth/mod.rs +++ b/src/server/routes/auth/mod.rs @@ -22,6 +22,8 @@ use crate::server::{ AppState, }; +use super::ErrorPage; + pub fn build() -> Router { Router::new() .route("/signup", get(signup_page).post(signup)) @@ -174,7 +176,7 @@ pub enum SignupError { impl IntoResponse for SignupError { fn into_response(self) -> axum::response::Response { - match self { + let (status, message) = match self { SignupError::InvalidEmail => (StatusCode::BAD_REQUEST, "Invalid Email"), SignupError::CsrfValidationFailed => { (StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again") @@ -183,8 +185,15 @@ impl IntoResponse for SignupError { error!(?e, "returning INTERNAL SERVER ERROR"); (StatusCode::INTERNAL_SERVER_ERROR, "Unknown Error") } - } - .into_response() + }; + + ( + status, + ErrorPage { + error: message.to_string(), + }, + ) + .into_response() } } @@ -311,9 +320,8 @@ impl IntoResponse for LoginError { ( status, - LoginPage { - error: Some(message.to_string()), - csrf_token: "".to_string(), + ErrorPage { + error: message.to_string(), }, ) .into_response() @@ -374,7 +382,7 @@ pub enum LogoutError { impl IntoResponse for LogoutError { fn into_response(self) -> axum::response::Response { - match self { + let (status, message) = match self { LogoutError::NotLoggedIn => (StatusCode::UNAUTHORIZED, "Unknown User"), LogoutError::CsrfValidationFailed => { (StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again") @@ -383,8 +391,15 @@ impl IntoResponse for LogoutError { error!(?e, "returning INTERNAL SERVER ERROR"); (StatusCode::INTERNAL_SERVER_ERROR, "Unknown Error") } - } - .into_response() + }; + + ( + status, + ErrorPage { + error: message.to_string(), + }, + ) + .into_response() } } @@ -456,7 +471,7 @@ pub enum SignupConfirmError { impl IntoResponse for SignupConfirmError { fn into_response(self) -> axum::response::Response { - match self { + let (status, message) = match self { SignupConfirmError::InvalidToken => (StatusCode::UNAUTHORIZED, "Unknown User"), SignupConfirmError::CsrfValidationFailed => { (StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again") @@ -465,7 +480,14 @@ impl IntoResponse for SignupConfirmError { error!(?e, "returning INTERNAL SERVER ERROR"); (StatusCode::INTERNAL_SERVER_ERROR, "Unknown Error") } - } - .into_response() + }; + + ( + status, + ErrorPage { + error: message.to_string(), + }, + ) + .into_response() } } diff --git a/src/server/routes/mod.rs b/src/server/routes/mod.rs index fd22d7a..e426331 100644 --- a/src/server/routes/mod.rs +++ b/src/server/routes/mod.rs @@ -29,3 +29,9 @@ async fn homepage(user: Auth) -> Homepage { is_logged_in: user.user.is_some(), } } + +#[derive(Template, WebTemplate)] +#[template(path = "error.html")] +struct ErrorPage { + error: String, +} diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..e41989c --- /dev/null +++ b/templates/error.html @@ -0,0 +1,5 @@ + + + Error +

{{error}}

+