feat: early redirects

This commit is contained in:
Per Stark
2024-12-28 01:00:16 +01:00
parent 20d0114f57
commit b4ae842e69
2 changed files with 32 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
use axum::{ use axum::{
extract::DefaultBodyLimit, extract::DefaultBodyLimit,
middleware::from_fn_with_state, middleware::from_fn_with_state,
routing::{get, post}, routing::{delete, get, post},
Router, Router,
}; };
use axum_session::{SessionConfig, SessionLayer, SessionStore}; use axum_session::{SessionConfig, SessionLayer, SessionStore};
@@ -24,7 +24,7 @@ use zettle_db::{
queue_length::queue_length_handler, queue_length::queue_length_handler,
}, },
html::{ html::{
account::{set_api_key, show_account_page}, account::{delete_account, set_api_key, show_account_page},
index::index_handler, index::index_handler,
search_result::search_result_handler, search_result::search_result_handler,
signin::{authenticate_user, show_signin_form}, signin::{authenticate_user, show_signin_form},
@@ -154,6 +154,7 @@ fn html_routes(
.route("/signin", get(show_signin_form).post(authenticate_user)) .route("/signin", get(show_signin_form).post(authenticate_user))
.route("/account", get(show_account_page)) .route("/account", get(show_account_page))
.route("/set-api-key", post(set_api_key)) .route("/set-api-key", post(set_api_key))
.route("/delete-account", delete(delete_account))
.route( .route(
"/signup", "/signup",
get(show_signup_form).post(process_signup_and_show_verification), get(show_signup_form).post(process_signup_and_show_verification),

View File

@@ -1,18 +1,18 @@
use axum::{ use axum::{
extract::State, extract::State,
http::{Response, StatusCode}, http::{Response, StatusCode, Uri},
response::{Html, IntoResponse, Redirect}, response::{Html, IntoResponse, Redirect},
}; };
use axum_htmx::HxRedirect;
use axum_session_auth::AuthSession; use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool; use axum_session_surreal::SessionSurrealPool;
use surrealdb::{engine::any::Any, Surreal}; use surrealdb::{engine::any::Any, Surreal};
use tracing::info;
use crate::{ use crate::{
error::ApiError, error::ApiError,
page_data, page_data,
server::{routes::html::render_template, AppState}, server::{routes::html::render_template, AppState},
storage::types::user::User, storage::{db::delete_item, types::user::User},
}; };
use super::render_block; use super::render_block;
@@ -25,17 +25,15 @@ pub async fn show_account_page(
State(state): State<AppState>, State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>, auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> { ) -> Result<impl IntoResponse, ApiError> {
if !auth.is_authenticated() { // Early return if the user is not authenticated
return Ok(Redirect::to("/").into_response()); let user = match auth.current_user {
} Some(user) => user,
None => return Ok(Redirect::to("/").into_response()),
info!("{:?}", auth.current_user); };
let output = render_template( let output = render_template(
AccountData::template_name(), AccountData::template_name(),
AccountData { AccountData { user },
user: auth.current_user.unwrap(),
},
state.templates, state.templates,
)?; )?;
@@ -47,15 +45,17 @@ pub async fn set_api_key(
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>, auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> { ) -> Result<impl IntoResponse, ApiError> {
// Early return if the user is not authenticated // 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 // Generate and set the API key
let api_key = User::set_api_key(&user.id, &state.surreal_db_client).await?; 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 // Update the user's API key
let updated_user = User { let updated_user = User {
api_key: Some(api_key), api_key: Some(api_key),
..user ..user.clone()
}; };
// Render the API key section block // Render the API key section block
@@ -68,3 +68,19 @@ pub async fn set_api_key(
Ok(output.into_response()) Ok(output.into_response())
} }
pub async fn delete_account(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
// Early return if the user is not authenticated
let user = auth.current_user.as_ref().ok_or(ApiError::AuthRequired)?;
delete_item::<User>(&state.surreal_db_client, &user.id).await?;
auth.logout_user();
auth.session.destroy();
Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())
}