Files
minne/html-router/src/routes/auth/signup.rs
T
Per Stark c6e499e5dc fix: harden html responses and cache chat sidebar data
Use strict template response handling and sanitized template user context, then add an in-process conversation archive cache with mutation-driven invalidation for chat sidebar renders.
2026-02-14 17:47:14 +01:00

64 lines
1.5 KiB
Rust

use axum::{extract::State, response::IntoResponse, Form};
use axum_htmx::HxBoosted;
use serde::{Deserialize, Serialize};
use common::storage::types::user::{Theme, User};
use crate::{
html_state::HtmlState,
middlewares::response_middleware::{HtmlError, TemplateResponse},
AuthSessionType,
};
#[derive(Deserialize, Serialize)]
pub struct SignupParams {
pub email: String,
pub password: String,
pub timezone: String,
}
pub async fn show_signup_form(
auth: AuthSessionType,
HxBoosted(boosted): HxBoosted,
) -> Result<impl IntoResponse, HtmlError> {
if auth.is_authenticated() {
return Ok(TemplateResponse::redirect("/"));
}
if boosted {
Ok(TemplateResponse::new_partial(
"auth/signup_form.html",
"body",
(),
))
} else {
Ok(TemplateResponse::new_template("auth/signup_form.html", ()))
}
}
pub async fn process_signup_and_show_verification(
State(state): State<HtmlState>,
auth: AuthSessionType,
Form(form): Form<SignupParams>,
) -> Result<impl IntoResponse, HtmlError> {
let user = match User::create_new(
form.email,
form.password,
&state.db,
form.timezone,
Theme::System.as_str().to_string(),
)
.await
{
Ok(user) => user,
Err(e) => {
tracing::error!("{:?}", e);
return Ok(TemplateResponse::bad_request(&e.to_string()).into_response());
}
};
auth.login_user(user.id);
Ok(TemplateResponse::redirect("/").into_response())
}