feat signout and reactivity

This commit is contained in:
Per Stark
2024-12-26 23:05:14 +01:00
parent fdfe4a05b8
commit 61e81ca894
7 changed files with 35 additions and 16 deletions

View File

@@ -26,6 +26,7 @@ use zettle_db::{
html::{ html::{
index::index_handler, index::index_handler,
search_result::search_result_handler, search_result::search_result_handler,
signout::sign_out_user,
signup::{process_signup_and_show_verification, show_signup_form}, signup::{process_signup_and_show_verification, show_signup_form},
}, },
}, },
@@ -147,6 +148,7 @@ fn html_routes(
Router::new() Router::new()
.route("/", get(index_handler)) .route("/", get(index_handler))
.route("/search", get(search_result_handler)) .route("/search", get(search_result_handler))
.route("/signout", get(sign_out_user))
.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

@@ -13,6 +13,7 @@ use crate::{
page_data!(IndexData, "index.html", { page_data!(IndexData, "index.html", {
queue_length: u32, queue_length: u32,
user: Option<User>
}); });
pub async fn index_handler( pub async fn index_handler(
@@ -21,11 +22,12 @@ pub async fn index_handler(
) -> Result<Html<String>, ApiError> { ) -> Result<Html<String>, ApiError> {
info!("Displaying index page"); info!("Displaying index page");
info!("{:?}", auth.current_user);
let queue_length = state.rabbitmq_consumer.get_queue_length().await?; let queue_length = state.rabbitmq_consumer.get_queue_length().await?;
let data = IndexData { queue_length }; 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(), data, state.templates)?;

View File

@@ -6,6 +6,7 @@ use minijinja_autoreload::AutoReloader;
pub mod index; pub mod index;
pub mod search_result; pub mod search_result;
pub mod signin; pub mod signin;
pub mod signout;
pub mod signup; pub mod signup;
pub trait PageData { pub trait PageData {

View File

@@ -0,0 +1,18 @@
use axum::response::{IntoResponse, Redirect};
use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool;
use surrealdb::{engine::any::Any, Surreal};
use crate::{error::ApiError, storage::types::user::User};
pub async fn sign_out_user(
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
if !auth.is_authenticated() {
return Ok(Redirect::to("/").into_response());
}
auth.logout_user();
Ok(Redirect::to("/").into_response())
}

View File

@@ -15,8 +15,7 @@
<label class="label"> <label class="label">
<span class="label-text">Email</span> <span class="label-text">Email</span>
</label> </label>
<input type="email" name="email" placeholder="Enter your email" class="input input-bordered w-full" required <input type="email" name="email" placeholder="Enter your email" class="input input-bordered w-full" required />
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" />
</div> </div>
<div class="form-control"> <div class="form-control">

View File

@@ -5,21 +5,17 @@
<body class="min-h-screen"> <body class="min-h-screen">
<nav class="navbar bg-base-200"> <nav class="navbar bg-base-200">
<div class="flex-1"> <div class="flex-1">
<a class="btn text-xl border-transparent btn-outline btn-primary" href="/">minnet</a> <a class="btn text-xl border-transparent btn-outline btn-primary" href="/">Minne</a>
</div> </div>
<div class="flex-none"> <div class="flex-none">
<ul class="menu menu-horizontal px-1"> <ul class="menu menu-horizontal px-1">
{% if user %}
<li><a hx-boost="true" href="/account">Account</a></li>
<li><a hx-boost="true" href="/signout">Sign out</a></li>
{% else %}
<li><a hx-boost="true" href="/signin">Login</a></li> <li><a hx-boost="true" href="/signin">Login</a></li>
<li><a hx-boost="true" href="/signup">Sign up</a></li> <li><a hx-boost="true" href="/signup">Sign up</a></li>
<li> {% endif %}
<details>
<summary>Parent</summary>
<ul class="bg-base-100 rounded-t-none p-2">
<li><a>Link 1</a></li>
<li><a>Link 2</a></li>
</ul>
</details>
</li>
</ul> </ul>
</div> </div>
</nav> </nav>

View File

@@ -4,10 +4,11 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}minnet{% endblock %}</title> <title>{% block title %}Minne{% endblock %}</title>
<script src="assets/htmx.min.js"></script> <script src="assets/htmx.min.js"></script>
<link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/style.css">
<link rel="manifest" href="/assets/manifest.json"> <link rel="manifest" href="/assets/manifest.json">
<!-- Optional but recommended for iOS support --> <!-- Optional but recommended for iOS support -->
<meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes">
<link rel="apple-touch-icon" href="/assets/icons/icon-192x192.png"> <link rel="apple-touch-icon" href="/assets/icons/icon-192x192.png">