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

@@ -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())
}
}
}