feat: account page

This commit is contained in:
Per Stark
2024-12-28 00:07:20 +01:00
parent a3984ab348
commit 53820e8b5f
8 changed files with 142 additions and 24 deletions

View File

@@ -0,0 +1,70 @@
use axum::{
extract::State,
http::{Response, StatusCode},
response::{Html, IntoResponse, Redirect},
};
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,
};
use super::render_block;
page_data!(AccountData, "auth/account.html", {
user: User
});
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);
let output = render_template(
AccountData::template_name(),
AccountData {
user: auth.current_user.unwrap(),
},
state.templates,
)?;
Ok(output.into_response())
}
pub async fn set_api_key(
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.ok_or(ApiError::AuthRequired)?;
// Generate and set the API key
let api_key = User::set_api_key(&user.id, &state.surreal_db_client).await?;
// Update the user's API key
let updated_user = User {
api_key: Some(api_key),
..user
};
// Render the API key section block
let output = render_block(
AccountData::template_name(),
"api_key_section",
AccountData { user: updated_user },
state.templates,
)?;
Ok(output.into_response())
}

View File

@@ -24,12 +24,14 @@ pub async fn index_handler(
let queue_length = state.rabbitmq_consumer.get_queue_length().await?;
let data = IndexData {
queue_length,
user: auth.current_user,
};
let output = render_template(IndexData::template_name(), data, state.templates)?;
let output = render_template(
IndexData::template_name(),
IndexData {
queue_length,
user: auth.current_user,
},
state.templates,
)?;
Ok(output)
}

View File

@@ -3,6 +3,7 @@ use std::sync::Arc;
use axum::response::Html;
use minijinja_autoreload::AutoReloader;
pub mod account;
pub mod index;
pub mod search_result;
pub mod signin;

View File

@@ -7,10 +7,9 @@ use axum::{
use axum_htmx::{HxBoosted, HxRedirect};
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 crate::{error::ApiError, page_data, server::AppState, storage::types::user::User};
use super::{render_block, render_template};
@@ -21,10 +20,7 @@ pub struct SignupParams {
pub remember_me: Option<String>,
}
#[derive(Serialize)]
struct PageData {
// name: String,
}
page_data!(ShowSignInForm, "auth/signin_form.html", {});
pub async fn show_signin_form(
State(state): State<AppState>,
@@ -36,12 +32,16 @@ pub async fn show_signin_form(
}
let output = match boosted {
true => render_block(
"auth/signin_form.html",
ShowSignInForm::template_name(),
"body",
PageData {},
ShowSignInForm {},
state.templates,
)?,
false => render_template(
ShowSignInForm::template_name(),
ShowSignInForm {},
state.templates,
)?,
false => render_template("auth/signin_form.html", PageData {}, state.templates)?,
};
Ok(output.into_response())
@@ -54,10 +54,7 @@ pub async fn authenticate_user(
) -> Result<impl IntoResponse, ApiError> {
let user = User::authenticate(form.email, form.password, &state.surreal_db_client).await?;
auth.login_user(user.id);
if form
.remember_me
.is_some_and(|string| string == "on".to_string())
{
if form.remember_me.is_some_and(|string| string == *"on") {
auth.remember_user(true);
}
Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())