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.
This commit is contained in:
azdle 2025-07-24 15:04:11 -05:00
parent b1bf7cb581
commit c7500ae6a3
3 changed files with 45 additions and 12 deletions

View file

@ -22,6 +22,8 @@ use crate::server::{
AppState, AppState,
}; };
use super::ErrorPage;
pub fn build() -> Router<AppState> { pub fn build() -> Router<AppState> {
Router::new() Router::new()
.route("/signup", get(signup_page).post(signup)) .route("/signup", get(signup_page).post(signup))
@ -174,7 +176,7 @@ pub enum SignupError {
impl IntoResponse for SignupError { impl IntoResponse for SignupError {
fn into_response(self) -> axum::response::Response { fn into_response(self) -> axum::response::Response {
match self { let (status, message) = match self {
SignupError::InvalidEmail => (StatusCode::BAD_REQUEST, "Invalid Email"), SignupError::InvalidEmail => (StatusCode::BAD_REQUEST, "Invalid Email"),
SignupError::CsrfValidationFailed => { SignupError::CsrfValidationFailed => {
(StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again") (StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again")
@ -183,8 +185,15 @@ impl IntoResponse for SignupError {
error!(?e, "returning INTERNAL SERVER ERROR"); error!(?e, "returning INTERNAL SERVER ERROR");
(StatusCode::INTERNAL_SERVER_ERROR, "Unknown 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, status,
LoginPage { ErrorPage {
error: Some(message.to_string()), error: message.to_string(),
csrf_token: "".to_string(),
}, },
) )
.into_response() .into_response()
@ -374,7 +382,7 @@ pub enum LogoutError {
impl IntoResponse for LogoutError { impl IntoResponse for LogoutError {
fn into_response(self) -> axum::response::Response { fn into_response(self) -> axum::response::Response {
match self { let (status, message) = match self {
LogoutError::NotLoggedIn => (StatusCode::UNAUTHORIZED, "Unknown User"), LogoutError::NotLoggedIn => (StatusCode::UNAUTHORIZED, "Unknown User"),
LogoutError::CsrfValidationFailed => { LogoutError::CsrfValidationFailed => {
(StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again") (StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again")
@ -383,8 +391,15 @@ impl IntoResponse for LogoutError {
error!(?e, "returning INTERNAL SERVER ERROR"); error!(?e, "returning INTERNAL SERVER ERROR");
(StatusCode::INTERNAL_SERVER_ERROR, "Unknown 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 { impl IntoResponse for SignupConfirmError {
fn into_response(self) -> axum::response::Response { fn into_response(self) -> axum::response::Response {
match self { let (status, message) = match self {
SignupConfirmError::InvalidToken => (StatusCode::UNAUTHORIZED, "Unknown User"), SignupConfirmError::InvalidToken => (StatusCode::UNAUTHORIZED, "Unknown User"),
SignupConfirmError::CsrfValidationFailed => { SignupConfirmError::CsrfValidationFailed => {
(StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again") (StatusCode::BAD_REQUEST, "CSRF Validation Failed, Try Again")
@ -465,7 +480,14 @@ impl IntoResponse for SignupConfirmError {
error!(?e, "returning INTERNAL SERVER ERROR"); error!(?e, "returning INTERNAL SERVER ERROR");
(StatusCode::INTERNAL_SERVER_ERROR, "Unknown Error") (StatusCode::INTERNAL_SERVER_ERROR, "Unknown Error")
} }
} };
.into_response()
(
status,
ErrorPage {
error: message.to_string(),
},
)
.into_response()
} }
} }

View file

@ -29,3 +29,9 @@ async fn homepage(user: Auth) -> Homepage {
is_logged_in: user.user.is_some(), is_logged_in: user.user.is_some(),
} }
} }
#[derive(Template, WebTemplate)]
#[template(path = "error.html")]
struct ErrorPage {
error: String,
}

5
templates/error.html Normal file
View file

@ -0,0 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<title>Error</title>
<p class="error">{{error}}</p>
</html>