refactor: use htmx target for registration
This commit is contained in:
parent
678690855a
commit
73c1b987cd
@ -20,7 +20,7 @@ async fn post(
|
|||||||
form: web::Form<ChangePasswordForm>,
|
form: web::Form<ChangePasswordForm>,
|
||||||
pool: web::Data<PgPool>,
|
pool: web::Data<PgPool>,
|
||||||
) -> Result<impl Responder, ApplicationError> {
|
) -> Result<impl Responder, ApplicationError> {
|
||||||
let is_dry = header.into_inner().is_some_and_equal("password-strength");
|
let is_dry = header.is_some_and_equal("password-strength");
|
||||||
|
|
||||||
let mut builder = PasswordChangeBuilder::<NoneToken>::new(
|
let mut builder = PasswordChangeBuilder::<NoneToken>::new(
|
||||||
pool.get_ref(),
|
pool.get_ref(),
|
||||||
|
@ -3,7 +3,7 @@ use maud::html;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::utils::{password_change::PasswordChangeBuilder, ApplicationError};
|
use crate::utils::{password_change::PasswordChangeBuilder, ApplicationError, HtmxTargetHeader};
|
||||||
use brass_db::models::Registration;
|
use brass_db::models::Registration;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -11,20 +11,17 @@ struct RegisterForm {
|
|||||||
token: String,
|
token: String,
|
||||||
password: String,
|
password: String,
|
||||||
passwordretyped: String,
|
passwordretyped: String,
|
||||||
dry: Option<bool>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/register")]
|
#[post("/register")]
|
||||||
async fn post(
|
async fn post(
|
||||||
form: web::Form<RegisterForm>,
|
form: web::Form<RegisterForm>,
|
||||||
pool: web::Data<PgPool>,
|
pool: web::Data<PgPool>,
|
||||||
|
header: web::Header<HtmxTargetHeader>,
|
||||||
) -> Result<impl Responder, ApplicationError> {
|
) -> Result<impl Responder, ApplicationError> {
|
||||||
// TODO: refactor into check if HX-TARGET = #password-strength exists
|
let is_dry = header.is_some_and_equal("password-strength");
|
||||||
let is_dry = form.dry.unwrap_or(false);
|
|
||||||
let token =
|
let Some(token) = Registration::does_token_exist(pool.get_ref(), &form.token).await? else {
|
||||||
if let Some(token) = Registration::does_token_exist(pool.get_ref(), &form.token).await? {
|
|
||||||
token
|
|
||||||
} else {
|
|
||||||
return Ok(HttpResponse::NoContent().finish());
|
return Ok(HttpResponse::NoContent().finish());
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -39,9 +36,15 @@ async fn post(
|
|||||||
let change = builder.build();
|
let change = builder.build();
|
||||||
|
|
||||||
let response = if is_dry {
|
let response = if is_dry {
|
||||||
change.validate_for_input().await.unwrap() // TODO
|
match change.validate_for_input().await {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => HttpResponse::UnprocessableEntity().body(e.message),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
change.validate().await.unwrap(); // TODO
|
if let Err(e) = change.validate().await {
|
||||||
|
return Ok(HttpResponse::UnprocessableEntity().body(e.message));
|
||||||
|
}
|
||||||
|
|
||||||
change.commit().await?;
|
change.commit().await?;
|
||||||
HttpResponse::Ok().body(
|
HttpResponse::Ok().body(
|
||||||
html! {
|
html! {
|
||||||
|
@ -11,9 +11,9 @@
|
|||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label" for="password">{{ new_password_label }}</label>
|
<label class="label" for="password">{{ new_password_label }}</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<input class="input" hx-trigger="keyup changed delay:500ms" hx-target="#password-strength"
|
<input class="input" hx-post="{{ endpoint }}" hx-trigger="keyup changed delay:500ms"
|
||||||
hx-target-422="#password-strength" placeholder="**********" name="password"
|
hx-target="#password-strength" hx-target-422="#password-strength" placeholder="**********"
|
||||||
type="password" required hx-swap="outerHTML" maxlength=256
|
name="password" type="password" required hx-swap="outerHTML" maxlength=256
|
||||||
_="on input put '' into #password-strength">
|
_="on input put '' into #password-strength">
|
||||||
</div>
|
</div>
|
||||||
<div id="password-strength" class="mb-3 help content"></div>
|
<div id="password-strength" class="mb-3 help content"></div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user