feat: early redirects

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

View File

@@ -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),

View File

@@ -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<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
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<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
// 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<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())
}