mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-21 00:49:54 +01:00
feat: account page
This commit is contained in:
@@ -24,6 +24,7 @@ use zettle_db::{
|
||||
queue_length::queue_length_handler,
|
||||
},
|
||||
html::{
|
||||
account::{set_api_key, show_account_page},
|
||||
index::index_handler,
|
||||
search_result::search_result_handler,
|
||||
signin::{authenticate_user, show_signin_form},
|
||||
@@ -151,6 +152,8 @@ fn html_routes(
|
||||
.route("/search", get(search_result_handler))
|
||||
.route("/signout", get(sign_out_user))
|
||||
.route("/signin", get(show_signin_form).post(authenticate_user))
|
||||
.route("/account", get(show_account_page))
|
||||
.route("/set-api-key", post(set_api_key))
|
||||
.route(
|
||||
"/signup",
|
||||
get(show_signup_form).post(process_signup_and_show_verification),
|
||||
|
||||
70
src/server/routes/html/account.rs
Normal file
70
src/server/routes/html/account.rs
Normal 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())
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -134,10 +134,6 @@ impl User {
|
||||
Err(ApiError::UserNotFound)
|
||||
}
|
||||
}
|
||||
pub async fn reset_api_key(id: &str, db: &SurrealDbClient) -> Result<String, ApiError> {
|
||||
// Simply call set_api_key to generate and set a new key
|
||||
Self::set_api_key(id, db).await
|
||||
}
|
||||
|
||||
pub async fn revoke_api_key(id: &str, db: &SurrealDbClient) -> Result<(), ApiError> {
|
||||
let user: Option<User> = db
|
||||
|
||||
49
templates/auth/account.html
Normal file
49
templates/auth/account.html
Normal file
@@ -0,0 +1,49 @@
|
||||
{% extends "body_base.html" %}
|
||||
{% block content %}
|
||||
<style>
|
||||
form.htmx-request {
|
||||
opacity: 0.5;
|
||||
}
|
||||
</style>
|
||||
<div class="min-h-screen grid place-items-center place-content-center">
|
||||
<div class="max-w-lg mx-auto">
|
||||
<h2 class="text-2xl font-bold text-center mb-8">Account Settings</h2>
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">Email</span>
|
||||
</label>
|
||||
<input type="email" name="email" value="{{ user.email }}" class="input input-bordered w-full" disabled />
|
||||
</div>
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">API key</span>
|
||||
</label>
|
||||
{% block api_key_section %}
|
||||
{% if user.api_key %}
|
||||
<input type="text" name="api-key" value="{{ user.api_key }}" class="input input-bordered w-full" disabled />
|
||||
{% else %}
|
||||
<button hx-post="/set-api-key" class="btn btn-secondary w-full" hx-swap="outerHTML">
|
||||
Create API-Key
|
||||
</button>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
</div>
|
||||
<div class="form-control mt-4">
|
||||
<button hx-post="/verify-email" class="btn btn-secondary w-full">
|
||||
Verify Email
|
||||
</button>
|
||||
</div>
|
||||
<div class="form-control mt-4">
|
||||
<button hx-get="/change-password" class="btn btn-primary w-full">
|
||||
Change Password
|
||||
</button>
|
||||
</div>
|
||||
<div class="form-control mt-4">
|
||||
<button hx-delete="/delete-account" class="btn btn-error w-full">
|
||||
Delete Account
|
||||
</button>
|
||||
</div>
|
||||
<div id="account-result" class="mt-4"></div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -20,7 +20,7 @@
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main class="container mx-auto px-4 py-8">
|
||||
<main class="container mx-auto ">
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user