HX-Redirect to handle redirections with htmx

This commit is contained in:
Alphonse Paix
2025-09-17 13:16:56 +02:00
parent 7689628ffb
commit 7364e2a23c
8 changed files with 41 additions and 95 deletions

View File

@@ -1,8 +1,9 @@
pub mod change_password;
pub mod dashboard;
pub mod newsletters;
mod change_password;
mod dashboard;
mod logout;
mod newsletters;
use crate::{routes::error_chain_fmt, session_state::TypedSession, templates::ErrorTemplate};
use crate::{routes::error_chain_fmt, templates::ErrorTemplate};
use askama::Template;
use axum::{
Json,
@@ -10,6 +11,7 @@ use axum::{
};
pub use change_password::*;
pub use dashboard::*;
pub use logout::*;
pub use newsletters::*;
use reqwest::StatusCode;
@@ -69,9 +71,3 @@ impl IntoResponse for AdminError {
}
}
}
#[tracing::instrument(name = "Logging out", skip(session))]
pub async fn logout(session: TypedSession) -> Result<Response, AdminError> {
session.clear().await;
Ok(Redirect::to("/login").into_response())
}

View File

@@ -1,26 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Change password</title>
</head>
<body>
<form action="/admin/password" method="post">
<input
type="password"
name="current_password"
placeholder="Current password"
/>
<input type="password" name="new_password" placeholder="New password" />
<input
type="password"
name="new_password_check"
placeholder="Confirm new password"
/>
<button type="submit">Change password</button>
</form>
{}
<p><a href="/admin/dashboard">Back</a></p>
</body>
</html>

View File

@@ -1,21 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Admin dashboard</title>
</head>
<body>
<p>Welcome {}!</p>
<p>Available actions:</p>
<ol>
<li><a href="/admin/password">Change password</a></li>
<li><a href="/admin/newsletters">Send a newsletter</a></li>
<li>
<form name="logoutForm" action="/admin/logout" method="post">
<input type="submit" value="Logout" />
</form>
</li>
</ol>
</body>
</html>

View File

@@ -1,19 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Send a newsletter</title>
</head>
<body>
<form action="/admin/newsletters" method="post">
<input type="text" name="title" placeholder="Subject" />
<input type="text" name="html" placeholder="Content (HTML)" />
<input type="text" name="text" placeholder="Content (text)" />
<input hidden type="text" name="idempotency_key" value="{}" />
<button type="submit">Send</button>
</form>
{}
<p><a href="/admin/dashboard">Back</a></p>
</body>
</html>

View File

@@ -0,0 +1,14 @@
use crate::{routes::AdminError, session_state::TypedSession};
use axum::{
http::HeaderMap,
response::{IntoResponse, Response},
};
use reqwest::StatusCode;
#[tracing::instrument(name = "Logging out", skip(session))]
pub async fn logout(session: TypedSession) -> Result<Response, AdminError> {
session.clear().await;
let mut headers = HeaderMap::new();
headers.insert("HX-Redirect", "/login".parse().unwrap());
Ok((StatusCode::OK, headers).into_response())
}

View File

@@ -6,12 +6,12 @@ use crate::{
templates::ErrorTemplate,
};
use askama::Template;
use axum::http::{HeaderMap, StatusCode};
use axum::{
Form, Json,
extract::State,
response::{Html, IntoResponse, Redirect, Response},
response::{Html, IntoResponse, Response},
};
use reqwest::StatusCode;
use secrecy::SecretString;
#[derive(thiserror::Error)]
@@ -75,7 +75,7 @@ pub async fn post_login(
connection_pool, ..
}): State<AppState>,
Form(form): Form<LoginFormData>,
) -> Result<Redirect, LoginError> {
) -> Result<Response, LoginError> {
let credentials = Credentials {
username: form.username.clone(),
password: form.password,
@@ -104,7 +104,10 @@ pub async fn post_login(
.insert_username(form.username)
.await
.map_err(|e| LoginError::UnexpectedError(e.into()))?;
Ok(Redirect::to("/admin/dashboard"))
let mut headers = HeaderMap::new();
headers.insert("HX-Redirect", "/admin/dashboard".parse().unwrap());
Ok((StatusCode::OK, headers).into_response())
}
}
}