Fixes to email verification process
Also make registration form progressively enhanced.
This commit is contained in:
parent
e59c6d596e
commit
609f6d3d9f
@ -1,7 +1,7 @@
|
||||
use axum::extract::{Query, State};
|
||||
use axum::response::Response;
|
||||
use axum::{Form, TypedHeader};
|
||||
use axum_login::{extractors::AuthContext, SqlxStore};
|
||||
use axum_login::SqlxStore;
|
||||
use lettre::SmtpTransport;
|
||||
use maud::html;
|
||||
use serde::Deserialize;
|
||||
@ -14,7 +14,7 @@ use crate::config::Config;
|
||||
use crate::error::{Error, Result};
|
||||
use crate::htmx::HXTarget;
|
||||
use crate::mailers::email_verification::send_confirmation_email;
|
||||
use crate::models::user::User;
|
||||
use crate::models::user::{AuthContext, User};
|
||||
use crate::models::user_email_verification_token::UserEmailVerificationToken;
|
||||
use crate::partials::confirm_email_form::{confirm_email_form, ConfirmEmailFormProps};
|
||||
use crate::partials::layout::Layout;
|
||||
@ -27,7 +27,7 @@ pub struct ConfirmEmailQuery {
|
||||
|
||||
pub async fn get(
|
||||
State(pool): State<PgPool>,
|
||||
auth: AuthContext<Uuid, User, SqlxStore<PgPool, User>>,
|
||||
auth: AuthContext,
|
||||
hx_target: Option<TypedHeader<HXTarget>>,
|
||||
layout: Layout,
|
||||
query: Query<ConfirmEmailQuery>,
|
||||
|
@ -91,5 +91,5 @@ pub async fn post(
|
||||
auth.login(&user)
|
||||
.await
|
||||
.map_err(|_| Error::InternalServerError)?;
|
||||
Ok(HXRedirect::to("/").into_response())
|
||||
Ok(HXRedirect::to("/").reload(true).into_response())
|
||||
}
|
||||
|
@ -44,17 +44,27 @@ pub async fn post(
|
||||
State(mailer): State<SmtpTransport>,
|
||||
State(config): State<Config>,
|
||||
mut auth: AuthContext,
|
||||
hx_target: Option<TypedHeader<HXTarget>>,
|
||||
layout: Layout,
|
||||
Form(register): Form<Register>,
|
||||
) -> Result<Response> {
|
||||
if register.password != register.password_confirmation {
|
||||
// return Err(Error::BadRequest("passwords do not match"));
|
||||
return Ok(register_form(RegisterFormProps {
|
||||
email: Some(register.email),
|
||||
name: register.name,
|
||||
password_error: Some("passwords do not match".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.into_response());
|
||||
return Ok(layout
|
||||
.with_subtitle("register")
|
||||
.targeted(hx_target)
|
||||
.render(html! {
|
||||
div class="center-horizontal" {
|
||||
header class="center-text" {
|
||||
h2 { "Register" }
|
||||
}
|
||||
(register_form(RegisterFormProps {
|
||||
email: Some(register.email),
|
||||
name: register.name,
|
||||
password_error: Some("passwords do not match".to_string()),
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
}));
|
||||
}
|
||||
let user = match User::create(
|
||||
&pool,
|
||||
@ -70,44 +80,62 @@ pub async fn post(
|
||||
Err(err) => {
|
||||
if let Error::InvalidEntity(validation_errors) = err {
|
||||
let field_errors = validation_errors.field_errors();
|
||||
return Ok(register_form(RegisterFormProps {
|
||||
email: Some(register.email),
|
||||
name: register.name,
|
||||
email_error: field_errors.get("email").map(|&errors| {
|
||||
errors
|
||||
.iter()
|
||||
.filter_map(|error| error.message.clone().map(|m| m.to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
}),
|
||||
name_error: field_errors.get("name").map(|&errors| {
|
||||
errors
|
||||
.iter()
|
||||
.filter_map(|error| error.message.clone().map(|m| m.to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
}),
|
||||
password_error: field_errors.get("password").map(|&errors| {
|
||||
errors
|
||||
.iter()
|
||||
.filter_map(|error| error.message.clone().map(|m| m.to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
}),
|
||||
..Default::default()
|
||||
})
|
||||
.into_response());
|
||||
return Ok(layout
|
||||
.with_subtitle("register")
|
||||
.targeted(hx_target)
|
||||
.render(html! {
|
||||
div class="center-horizontal" {
|
||||
header class="center-text" {
|
||||
h2 { "Register" }
|
||||
}
|
||||
(register_form(RegisterFormProps {
|
||||
email: Some(register.email),
|
||||
name: register.name,
|
||||
email_error: field_errors.get("email").map(|&errors| {
|
||||
errors
|
||||
.iter()
|
||||
.filter_map(|error| error.message.clone().map(|m| m.to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
}),
|
||||
name_error: field_errors.get("name").map(|&errors| {
|
||||
errors
|
||||
.iter()
|
||||
.filter_map(|error| error.message.clone().map(|m| m.to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
}),
|
||||
password_error: field_errors.get("password").map(|&errors| {
|
||||
errors
|
||||
.iter()
|
||||
.filter_map(|error| error.message.clone().map(|m| m.to_string()))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
}),
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
}));
|
||||
}
|
||||
if let Error::Sqlx(sqlx::error::Error::Database(db_error)) = &err {
|
||||
if let Some(constraint) = db_error.constraint() {
|
||||
if constraint == "users_email_idx" {
|
||||
return Ok(register_form(RegisterFormProps {
|
||||
email: Some(register.email),
|
||||
name: register.name,
|
||||
email_error: Some("email already exists".to_string()),
|
||||
..Default::default()
|
||||
})
|
||||
.into_response());
|
||||
return Ok(layout
|
||||
.with_subtitle("register")
|
||||
.targeted(hx_target)
|
||||
.render(html! {
|
||||
div class="center-horizontal" {
|
||||
header class="center-text" {
|
||||
h2 { "Register" }
|
||||
}
|
||||
(register_form(RegisterFormProps {
|
||||
email: Some(register.email),
|
||||
name: register.name,
|
||||
email_error: Some("email already exists".to_string()),
|
||||
..Default::default()
|
||||
}))
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -120,5 +148,5 @@ pub async fn post(
|
||||
auth.login(&user)
|
||||
.await
|
||||
.map_err(|_| Error::InternalServerError)?;
|
||||
Ok(HXRedirect::to("/").into_response())
|
||||
Ok(HXRedirect::to("/").reload(true).into_response())
|
||||
}
|
||||
|
27
src/htmx.rs
27
src/htmx.rs
@ -3,6 +3,8 @@ use headers::{self, Header};
|
||||
use http::header::{HeaderName, HeaderValue};
|
||||
use http::StatusCode;
|
||||
|
||||
#[allow(clippy::declare_interior_mutable_const)]
|
||||
pub const HX_REDIRECT: HeaderName = HeaderName::from_static("hx-redirect");
|
||||
#[allow(clippy::declare_interior_mutable_const)]
|
||||
pub const HX_LOCATION: HeaderName = HeaderName::from_static("hx-location");
|
||||
#[allow(clippy::declare_interior_mutable_const)]
|
||||
@ -16,23 +18,38 @@ pub const HX_TARGET: HeaderName = HeaderName::from_static("hx-target");
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HXRedirect {
|
||||
location: HeaderValue,
|
||||
reload: bool,
|
||||
}
|
||||
|
||||
impl HXRedirect {
|
||||
pub fn to(uri: &str) -> Self {
|
||||
Self {
|
||||
location: HeaderValue::try_from(uri).expect("URI isn't a valid header value"),
|
||||
reload: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reload(mut self, reload: bool) -> Self {
|
||||
self.reload = reload;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for HXRedirect {
|
||||
fn into_response(self) -> Response {
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(HX_LOCATION, self.location)],
|
||||
)
|
||||
.into_response()
|
||||
if self.reload {
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(HX_REDIRECT, self.location)],
|
||||
)
|
||||
.into_response()
|
||||
} else {
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(HX_LOCATION, self.location)],
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,12 @@ pub fn header(title: &str, user: Option<User>) -> Markup {
|
||||
}
|
||||
div class="auth" {
|
||||
@if let Some(user) = user {
|
||||
(user_name(user))
|
||||
(user_name(user.clone()))
|
||||
@if !user.email_verified {
|
||||
span { " (" }
|
||||
a href="/confirm-email" { "unverified" }
|
||||
span { ")" }
|
||||
}
|
||||
span { " | " }
|
||||
a href="/logout" { "logout" }
|
||||
} @else {
|
||||
|
@ -10,12 +10,10 @@ use axum::{
|
||||
response::{IntoResponse, Response},
|
||||
TypedHeader,
|
||||
};
|
||||
use axum_login::{extractors::AuthContext, SqlxStore};
|
||||
use headers::HeaderValue;
|
||||
use maud::{html, Markup, DOCTYPE};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::models::user::AuthContext;
|
||||
use crate::models::user::User;
|
||||
use crate::config::Config;
|
||||
use crate::htmx::HXTarget;
|
||||
@ -45,7 +43,7 @@ where
|
||||
.await
|
||||
.map_err(|err| err.into_response())?;
|
||||
let auth_context =
|
||||
AuthContext::<Uuid, User, SqlxStore<PgPool, User>>::from_request_parts(parts, state)
|
||||
AuthContext::from_request_parts(parts, state)
|
||||
.await
|
||||
.map_err(|err| err.into_response())?;
|
||||
Ok(Self {
|
||||
|
@ -20,7 +20,7 @@ pub fn register_form(props: RegisterFormProps) -> Markup {
|
||||
general_error,
|
||||
} = props;
|
||||
html! {
|
||||
form hx-post="/register" hx-swap="outerHTML" class="auth-form-grid" {
|
||||
form action="/register" method="post" class="auth-form-grid" {
|
||||
label for="email" { "Email *" }
|
||||
input type="email" name="email" id="email" placeholder="Email" value=(email.unwrap_or_default()) required;
|
||||
@if let Some(email_error) = email_error {
|
||||
|
Loading…
Reference in New Issue
Block a user