add function to be able to delete names

This commit is contained in:
2025-08-21 12:31:31 +02:00
parent c74500adfd
commit a0eddece86
8 changed files with 305 additions and 40 deletions

112
src/admin.rs Normal file
View File

@@ -0,0 +1,112 @@
use crate::{language::language, page::Page, AppState};
use axum::{
extract::State,
http::HeaderMap,
response::{IntoResponse, Redirect, Response},
routing::{get, post},
Form, Router,
};
use axum_extra::extract::{
cookie::{Cookie, Expiration},
CookieJar, PrivateCookieJar,
};
use maud::{html, Markup};
use serde::Deserialize;
use time::OffsetDateTime;
#[derive(Deserialize)]
struct LoginForm {
password: String,
}
async fn login_page(cookies: CookieJar, headers: HeaderMap) -> Markup {
let lang = language(&cookies, &headers);
rust_i18n::set_locale(lang.to_locale());
Page::new(lang).content(html! {
h1 { "Admin Login" }
form method="POST" action="/admin/login" {
fieldset {
label for="password" { "Password:" }
input
type="password"
name="password"
id="password"
required;
input type="submit" value="Login";
}
}
})
}
async fn login(
State(state): State<AppState>,
private_cookies: PrivateCookieJar,
Form(form): Form<LoginForm>,
) -> Response {
if form.password == state.admin_password {
// Set secure admin session cookie
let expiration_date = OffsetDateTime::now_utc() + time::Duration::days(30);
let mut cookie = Cookie::new("admin_session", "authenticated");
cookie.set_expires(Expiration::DateTime(expiration_date));
cookie.set_http_only(true);
cookie.set_secure(true);
cookie.set_path("/");
let updated_cookies = private_cookies.add(cookie);
(updated_cookies, Redirect::to("/protected")).into_response()
} else {
// Invalid password, redirect back to login
Redirect::to("/admin/login").into_response()
}
}
async fn logout(private_cookies: PrivateCookieJar) -> Response {
// Remove admin session cookie
let expired_cookie = Cookie::build(("admin_session", ""))
.expires(Expiration::DateTime(
OffsetDateTime::now_utc() - time::Duration::days(1),
))
.http_only(true)
.secure(true)
.path("/")
.build();
let updated_cookies = private_cookies.add(expired_cookie);
(updated_cookies, Redirect::to("/")).into_response()
}
async fn protected_page(
private_cookies: PrivateCookieJar,
cookies: CookieJar,
headers: HeaderMap,
) -> Response {
// Check if admin is authenticated
if private_cookies.get("admin_session").is_none() {
return Redirect::to("/admin/login").into_response();
}
let lang = language(&cookies, &headers);
rust_i18n::set_locale(lang.to_locale());
let markup = Page::new(lang).content(html! {
h1 { "Protected Admin Area" }
p { "Welcome to the admin area! This is a protected route." }
p { "Only authenticated administrators can access this page." }
form method="POST" action="/admin/logout" {
input type="submit" value="Logout" class="secondary";
}
});
markup.into_response()
}
pub fn routes() -> Router<AppState> {
Router::new()
.route("/admin/login", get(login_page))
.route("/admin/login", post(login))
.route("/admin/logout", post(logout))
.route("/protected", get(protected_page))
}