From bdf7a12e9c3070a10c6a988da43c11b2441e028e Mon Sep 17 00:00:00 2001 From: Per Stark Date: Sat, 28 Dec 2024 01:00:16 +0100 Subject: [PATCH] feat: early redirects --- src/bin/server.rs | 5 ++-- src/server/routes/html/account.rs | 42 +++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/bin/server.rs b/src/bin/server.rs index fbd77e9..04fae10 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -1,7 +1,7 @@ use axum::{ extract::DefaultBodyLimit, middleware::from_fn_with_state, - routing::{get, post}, + routing::{delete, get, post}, Router, }; use axum_session::{SessionConfig, SessionLayer, SessionStore}; @@ -24,7 +24,7 @@ use zettle_db::{ queue_length::queue_length_handler, }, html::{ - account::{set_api_key, show_account_page}, + account::{delete_account, set_api_key, show_account_page}, index::index_handler, search_result::search_result_handler, signin::{authenticate_user, show_signin_form}, @@ -154,6 +154,7 @@ fn html_routes( .route("/signin", get(show_signin_form).post(authenticate_user)) .route("/account", get(show_account_page)) .route("/set-api-key", post(set_api_key)) + .route("/delete-account", delete(delete_account)) .route( "/signup", get(show_signup_form).post(process_signup_and_show_verification), diff --git a/src/server/routes/html/account.rs b/src/server/routes/html/account.rs index a57b743..e25cff3 100644 --- a/src/server/routes/html/account.rs +++ b/src/server/routes/html/account.rs @@ -1,18 +1,18 @@ use axum::{ extract::State, - http::{Response, StatusCode}, + http::{Response, StatusCode, Uri}, response::{Html, IntoResponse, Redirect}, }; +use axum_htmx::HxRedirect; use axum_session_auth::AuthSession; use axum_session_surreal::SessionSurrealPool; use surrealdb::{engine::any::Any, Surreal}; -use tracing::info; use crate::{ error::ApiError, page_data, server::{routes::html::render_template, AppState}, - storage::types::user::User, + storage::{db::delete_item, types::user::User}, }; use super::render_block; @@ -25,17 +25,15 @@ pub async fn show_account_page( State(state): State, auth: AuthSession, Surreal>, ) -> Result { - if !auth.is_authenticated() { - return Ok(Redirect::to("/").into_response()); - } - - info!("{:?}", auth.current_user); + // Early return if the user is not authenticated + let user = match auth.current_user { + Some(user) => user, + None => return Ok(Redirect::to("/").into_response()), + }; let output = render_template( AccountData::template_name(), - AccountData { - user: auth.current_user.unwrap(), - }, + AccountData { user }, state.templates, )?; @@ -47,15 +45,17 @@ pub async fn set_api_key( auth: AuthSession, Surreal>, ) -> Result { // Early return if the user is not authenticated - let user = auth.current_user.ok_or(ApiError::AuthRequired)?; + let user = auth.current_user.as_ref().ok_or(ApiError::AuthRequired)?; // Generate and set the API key let api_key = User::set_api_key(&user.id, &state.surreal_db_client).await?; + auth.cache_clear_user(user.id.to_string()); + // Update the user's API key let updated_user = User { api_key: Some(api_key), - ..user + ..user.clone() }; // Render the API key section block @@ -68,3 +68,19 @@ pub async fn set_api_key( Ok(output.into_response()) } + +pub async fn delete_account( + State(state): State, + auth: AuthSession, Surreal>, +) -> Result { + // Early return if the user is not authenticated + let user = auth.current_user.as_ref().ok_or(ApiError::AuthRequired)?; + + delete_item::(&state.surreal_db_client, &user.id).await?; + + auth.logout_user(); + + auth.session.destroy(); + + Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response()) +}