Confirmation page and minor improvements to homepage and form messages

Basic redirect with flash messages for success and error messages
This commit is contained in:
Alphonse Paix
2025-09-16 16:47:28 +02:00
parent 612a221907
commit 8e1d68d948
12 changed files with 102 additions and 75 deletions

View File

@@ -2,7 +2,6 @@ mod admin;
mod health_check;
mod home;
mod login;
mod register;
mod subscriptions;
mod subscriptions_confirm;
@@ -10,6 +9,5 @@ pub use admin::*;
pub use health_check::*;
pub use home::*;
pub use login::*;
pub use register::*;
pub use subscriptions::*;
pub use subscriptions_confirm::*;

View File

@@ -1,11 +1,19 @@
use askama::Template;
use axum::response::Html;
use axum_messages::Messages;
#[derive(Template)]
#[template(path = "../templates/home.html")]
struct HomeTemplate;
struct HomeTemplate {
message: String,
}
pub async fn home() -> Html<String> {
let template = HomeTemplate;
pub async fn home(messages: Messages) -> Html<String> {
let template = HomeTemplate {
message: messages
.last()
.map(|msg| msg.to_string())
.unwrap_or_default(),
};
Html(template.render().unwrap())
}

View File

@@ -13,7 +13,6 @@ use axum::{
use axum_messages::Messages;
use reqwest::StatusCode;
use secrecy::SecretString;
use std::fmt::Write;
#[derive(thiserror::Error)]
pub enum LoginError {
@@ -54,7 +53,7 @@ impl IntoResponse for LoginError {
#[derive(Template)]
#[template(path = "../templates/login.html")]
struct LoginTemplate {
error_html: String,
error: String,
}
#[derive(serde::Deserialize)]
@@ -64,11 +63,12 @@ pub struct LoginFormData {
}
pub async fn get_login(messages: Messages) -> Html<String> {
let mut error_html = String::new();
for message in messages {
writeln!(error_html, "<p><i>{}</i></p>", message).unwrap();
}
let template = LoginTemplate { error_html };
let template = LoginTemplate {
error: messages
.last()
.map(|msg| msg.to_string())
.unwrap_or_default(),
};
Html(template.render().unwrap())
}

View File

@@ -1,11 +0,0 @@
use axum::response::{Html, IntoResponse, Response};
use axum_messages::Messages;
use std::fmt::Write;
pub async fn register(messages: Messages) -> Response {
let mut error_html = String::new();
for message in messages {
writeln!(error_html, "<p><i>{}</i></p>", message).unwrap();
}
Html(format!(include_str!("register/register.html"), error_html)).into_response()
}

View File

@@ -1,11 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Account confirmed</title>
</head>
<body>
<p>Your account has been confirmed. Welcome!</p>
</body>
</html>

View File

@@ -1,22 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Register</title>
</head>
<body>
<form action="/subscriptions" method="post">
<input type="text" name="name" placeholder="Name" />
<input type="text" name="email" placeholder="Email address" />
<input
type="text"
name="email_check"
placeholder="Confirm email address"
/>
<button type="Register">Register</button>
</form>
{}
<p><a href="/">Back</a></p>
</body>
</html>

View File

@@ -72,7 +72,7 @@ impl IntoResponse for SubscribeError {
}),
)
.into_response(),
SubscribeError::ValidationError(_) => Redirect::to("/register").into_response(),
SubscribeError::ValidationError(_) => Redirect::to("/").into_response(),
}
}
}
@@ -125,7 +125,7 @@ pub async fn subscribe(
.await
.context("Failed to commit the database transaction to store a new subscriber.")?;
messages.success("A confirmation email has been sent.");
Ok(Redirect::to("/register").into_response())
Ok(Redirect::to("/").into_response())
}
#[tracing::instrument(

View File

@@ -1,4 +1,5 @@
use crate::startup::AppState;
use askama::Template;
use axum::{
extract::{Query, State},
http::StatusCode,
@@ -8,6 +9,10 @@ use serde::Deserialize;
use sqlx::PgPool;
use uuid::Uuid;
#[derive(Template)]
#[template(path = "../templates/confirm.html")]
struct ConfirmTemplate;
#[tracing::instrument(name = "Confirming new subscriber", skip(params))]
pub async fn confirm(
State(AppState {
@@ -27,7 +32,7 @@ pub async fn confirm(
{
StatusCode::INTERNAL_SERVER_ERROR.into_response()
} else {
Html(include_str!("register/confirm.html")).into_response()
Html(ConfirmTemplate.render().unwrap()).into_response()
}
} else {
StatusCode::UNAUTHORIZED.into_response()

View File

@@ -127,7 +127,6 @@ pub fn app(
.layer(middleware::from_fn(require_auth));
Router::new()
.route("/", get(home))
.route("/register", get(register))
.route("/login", get(get_login).post(post_login))
.route("/health_check", get(health_check))
.route("/subscriptions", post(subscribe))