Use HTML swap to display success and error messages

This commit is contained in:
Alphonse Paix
2025-09-17 03:40:23 +02:00
parent 88dad022ce
commit 2d336ed000
18 changed files with 134 additions and 232 deletions

View File

@@ -3,6 +3,7 @@ use crate::{
routes::error_chain_fmt,
session_state::TypedSession,
startup::AppState,
templates::ErrorTemplate,
};
use askama::Template;
use axum::{
@@ -10,7 +11,6 @@ use axum::{
extract::State,
response::{Html, IntoResponse, Redirect, Response},
};
use axum_messages::Messages;
use reqwest::StatusCode;
use secrecy::SecretString;
@@ -45,16 +45,19 @@ impl IntoResponse for LoginError {
}),
)
.into_response(),
LoginError::AuthError(_) => Redirect::to("/login").into_response(),
LoginError::AuthError(e) => {
let template = ErrorTemplate {
error_message: e.to_string(),
};
Html(template.render().unwrap()).into_response()
}
}
}
}
#[derive(Template)]
#[template(path = "../templates/login.html")]
struct LoginTemplate {
error: String,
}
struct LoginTemplate;
#[derive(serde::Deserialize)]
pub struct LoginFormData {
@@ -62,19 +65,12 @@ pub struct LoginFormData {
password: SecretString,
}
pub async fn get_login(messages: Messages) -> Html<String> {
let template = LoginTemplate {
error: messages
.last()
.map(|msg| msg.to_string())
.unwrap_or_default(),
};
Html(template.render().unwrap())
pub async fn get_login() -> Html<String> {
Html(LoginTemplate.render().unwrap())
}
pub async fn post_login(
session: TypedSession,
messages: Messages,
State(AppState {
connection_pool, ..
}): State<AppState>,
@@ -89,11 +85,7 @@ pub async fn post_login(
Err(e) => {
let e = match e {
AuthError::UnexpectedError(_) => LoginError::UnexpectedError(e.into()),
AuthError::InvalidCredentials(_) => {
let e = LoginError::AuthError(e.into());
messages.error(e.to_string());
e
}
AuthError::InvalidCredentials(_) => LoginError::AuthError(e.into()),
AuthError::NotAuthenticated => unreachable!(),
};
Err(e)