From b736e2fe8df1d0c8f1cbcec7a075c9bfc183ae6d Mon Sep 17 00:00:00 2001 From: Alphonse Paix Date: Tue, 16 Sep 2025 16:47:28 +0200 Subject: [PATCH] Confirmation page and minor improvements to homepage and form messages Basic redirect with flash messages for success and error messages --- src/routes.rs | 2 - src/routes/home.rs | 14 ++++-- src/routes/login.rs | 14 +++--- src/routes/register.rs | 11 ----- src/routes/register/confirm.html | 11 ----- src/routes/register/register.html | 22 ---------- src/routes/subscriptions.rs | 4 +- src/routes/subscriptions_confirm.rs | 7 ++- src/startup.rs | 1 - templates/confirm.html | 67 +++++++++++++++++++++++++++++ templates/home.html | 20 +++------ templates/login.html | 4 +- 12 files changed, 102 insertions(+), 75 deletions(-) delete mode 100644 src/routes/register.rs delete mode 100644 src/routes/register/confirm.html delete mode 100644 src/routes/register/register.html create mode 100644 templates/confirm.html diff --git a/src/routes.rs b/src/routes.rs index 71206e1..772876f 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -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::*; diff --git a/src/routes/home.rs b/src/routes/home.rs index 8de5fb4..5f36524 100644 --- a/src/routes/home.rs +++ b/src/routes/home.rs @@ -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 { - let template = HomeTemplate; +pub async fn home(messages: Messages) -> Html { + let template = HomeTemplate { + message: messages + .last() + .map(|msg| msg.to_string()) + .unwrap_or_default(), + }; Html(template.render().unwrap()) } diff --git a/src/routes/login.rs b/src/routes/login.rs index ab16eb8..d004f7d 100644 --- a/src/routes/login.rs +++ b/src/routes/login.rs @@ -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 { - let mut error_html = String::new(); - for message in messages { - writeln!(error_html, "

{}

", 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()) } diff --git a/src/routes/register.rs b/src/routes/register.rs deleted file mode 100644 index 1c9fb12..0000000 --- a/src/routes/register.rs +++ /dev/null @@ -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, "

{}

", message).unwrap(); - } - Html(format!(include_str!("register/register.html"), error_html)).into_response() -} diff --git a/src/routes/register/confirm.html b/src/routes/register/confirm.html deleted file mode 100644 index 48dc57a..0000000 --- a/src/routes/register/confirm.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - Account confirmed - - -

Your account has been confirmed. Welcome!

- - diff --git a/src/routes/register/register.html b/src/routes/register/register.html deleted file mode 100644 index 82d576e..0000000 --- a/src/routes/register/register.html +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - Register - - -
- - - - -
- {} -

Back

- - diff --git a/src/routes/subscriptions.rs b/src/routes/subscriptions.rs index 6302a94..2711cfc 100644 --- a/src/routes/subscriptions.rs +++ b/src/routes/subscriptions.rs @@ -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( diff --git a/src/routes/subscriptions_confirm.rs b/src/routes/subscriptions_confirm.rs index 45bf90e..3d94e6a 100644 --- a/src/routes/subscriptions_confirm.rs +++ b/src/routes/subscriptions_confirm.rs @@ -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() diff --git a/src/startup.rs b/src/startup.rs index bd208df..61d5b2b 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -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)) diff --git a/templates/confirm.html b/templates/confirm.html new file mode 100644 index 0000000..76e04fa --- /dev/null +++ b/templates/confirm.html @@ -0,0 +1,67 @@ +{% extends "base.html" %} {% block title %}zero2prod{% endblock %} {% block +content %} +
+
+
+
+ + + +
+ +

+ Subscription confirmed! +

+

+ Your email has been confirmed. You're all set to receive our newsletter + updates. +

+
+ +
+
+
+ + + +
+

+ Welcome to our newsletter! You'll receive quality content about + Rust backend development straight to your inbox. +

+
+
+
+ + +
+
+ {% endblock %} +
diff --git a/templates/home.html b/templates/home.html index 0bfcf5c..45dbe25 100644 --- a/templates/home.html +++ b/templates/home.html @@ -7,7 +7,7 @@ block content %}

zero2prod

- Welcome to my newsletter! Stay updated on my latest projects and + Welcome to our newsletter! Stay updated on our latest projects and thoughts. Unsubscribe at any time.

@@ -28,7 +28,6 @@ block content %}
-

Stay updated

- Subscribe to my newsletter to get the latest updates. + Subscribe to our newsletter to get the latest updates.

-
+
-
+
{{ message }}
@@ -159,12 +153,12 @@ block content %}
-
0
+
0
email opened
-
~1ms
-
response time
+
3
+
issues delivered
diff --git a/templates/login.html b/templates/login.html index be1d13f..4a35d09 100644 --- a/templates/login.html +++ b/templates/login.html @@ -5,7 +5,7 @@ block content %}

Login

- Sign in to access the admin dashboard + Sign in to access the admin dashboard.

@@ -53,7 +53,7 @@ block content %} -
{{ error_html }}
+
{{ error }}