refactoring route names

This commit is contained in:
Per Stark
2024-12-23 10:18:25 +01:00
parent 3f32e9a500
commit 1f3e5a265b
7 changed files with 66 additions and 128 deletions

View File

@@ -24,9 +24,9 @@ use zettle_db::{
queue_length::queue_length_handler,
},
html::{
auth::{show_signup_form, signup_handler},
index::index_handler,
search_result::search_result_handler,
signup::{process_signup_and_show_verification, show_signup_form},
},
},
AppState,
@@ -147,7 +147,10 @@ fn html_routes(
Router::new()
.route("/", get(index_handler))
.route("/search", get(search_result_handler))
.route("/signup", get(show_signup_form).post(signup_handler))
.route(
"/signup",
get(show_signup_form).post(process_signup_and_show_verification),
)
.nest_service("/assets", ServeDir::new("assets/"))
.layer(
AuthSessionLayer::<User, String, SessionSurrealPool<Any>, Surreal<Any>>::new(Some(

View File

@@ -3,9 +3,10 @@ use std::sync::Arc;
use axum::response::Html;
use minijinja_autoreload::AutoReloader;
pub mod auth;
pub mod index;
pub mod search_result;
pub mod signin;
pub mod signup;
pub trait PageData {
fn template_name() -> &'static str;

View File

@@ -0,0 +1,56 @@
use axum::{
extract::State,
response::{IntoResponse, Redirect},
Form,
};
use axum_htmx::HxBoosted;
use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool;
use serde::{Deserialize, Serialize};
use surrealdb::{engine::any::Any, Surreal};
use crate::{error::ApiError, server::AppState, storage::types::user::User};
use super::{render_block, render_template};
#[derive(Deserialize, Serialize)]
pub struct SignupParams {
pub email: String,
pub password: String,
}
#[derive(Serialize)]
struct PageData {
// name: String,
}
pub async fn show_login_form(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
HxBoosted(boosted): HxBoosted,
) -> Result<impl IntoResponse, ApiError> {
if auth.is_authenticated() {
return Ok(Redirect::to("/").into_response());
}
let output = match boosted {
true => render_block(
"auth/signup_form.html",
"body",
PageData {},
state.templates,
)?,
false => render_template("auth/signup_form.html", PageData {}, state.templates)?,
};
Ok(output.into_response())
}
pub async fn authenticate_user(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
Form(form): Form<SignupParams>,
) -> Result<impl IntoResponse, ApiError> {
let user = User::create_new(form.email, form.password, &state.surreal_db_client).await?;
auth.login_user(user.id);
Ok(())
}

View File

@@ -45,7 +45,7 @@ pub async fn show_signup_form(
Ok(output.into_response())
}
pub async fn signup_handler(
pub async fn process_signup_and_show_verification(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
Form(form): Form<SignupParams>,