Fix login and add feed form

Add feed form still WIP
This commit is contained in:
Tyler Hallada 2023-11-12 18:40:05 -05:00
parent 575f7d45b9
commit ea4784ff07
3 changed files with 31 additions and 29 deletions

View File

@ -58,8 +58,10 @@ pub async fn get(
#[derive(Deserialize)]
pub struct AddFeed {
url: String,
#[serde(default)]
#[serde_as(as = "NoneAsEmptyString")]
title: Option<String>,
#[serde(default)]
#[serde_as(as = "NoneAsEmptyString")]
description: Option<String>,
}

View File

@ -9,7 +9,7 @@ use tracing::info;
use crate::auth::verify_password;
use crate::error::{Error, Result};
use crate::htmx::{HXRedirect, HXTarget};
use crate::htmx::{HXRedirect, HXTarget, HXRequest};
use crate::partials::login_form::{login_form, LoginFormProps};
use crate::{
models::user::{AuthContext, User},
@ -50,6 +50,7 @@ pub async fn post(
State(pool): State<PgPool>,
mut auth: AuthContext,
hx_target: Option<TypedHeader<HXTarget>>,
hx_request: Option<TypedHeader<HXRequest>>,
layout: Layout,
Form(login): Form<Login>,
) -> Result<Response> {
@ -91,5 +92,8 @@ pub async fn post(
auth.login(&user)
.await
.map_err(|_| Error::InternalServerError)?;
Ok(HXRedirect::to("/").reload(true).into_response())
Ok(HXRedirect::to("/")
.is_htmx(hx_request.is_some())
.reload(true)
.into_response())
}

View File

@ -1,4 +1,4 @@
use axum::response::{IntoResponse, Response};
use axum::response::{IntoResponse, Redirect, Response};
use headers::{self, Header};
use http::header::{HeaderName, HeaderValue};
use http::StatusCode;
@ -19,6 +19,7 @@ pub const HX_TARGET: HeaderName = HeaderName::from_static("hx-target");
pub struct HXRedirect {
location: HeaderValue,
reload: bool,
is_htmx_request: bool,
}
impl HXRedirect {
@ -26,6 +27,7 @@ impl HXRedirect {
Self {
location: HeaderValue::try_from(uri).expect("URI isn't a valid header value"),
reload: false,
is_htmx_request: true,
}
}
@ -33,22 +35,23 @@ impl HXRedirect {
self.reload = reload;
self
}
pub fn is_htmx(mut self, is_htmx: bool) -> Self {
self.is_htmx_request = is_htmx;
self
}
}
impl IntoResponse for HXRedirect {
fn into_response(self) -> Response {
if self.is_htmx_request {
if self.reload {
(
StatusCode::OK,
[(HX_REDIRECT, self.location)],
)
.into_response()
(StatusCode::OK, [(HX_REDIRECT, self.location)]).into_response()
} else {
(
StatusCode::OK,
[(HX_LOCATION, self.location)],
)
.into_response()
(StatusCode::OK, [(HX_LOCATION, self.location)]).into_response()
}
} else {
Redirect::to(self.location.to_str().unwrap()).into_response()
}
}
}
@ -72,9 +75,7 @@ impl Header for HXRequest {
where
I: Iterator<Item = &'i HeaderValue>,
{
let value = values
.next()
.ok_or_else(headers::Error::invalid)?;
let value = values.next().ok_or_else(headers::Error::invalid)?;
if value == "true" {
Ok(HXRequest(true))
@ -89,11 +90,7 @@ impl Header for HXRequest {
where
E: Extend<HeaderValue>,
{
let s = if self.0 {
"true"
} else {
"false"
};
let s = if self.0 { "true" } else { "false" };
let value = HeaderValue::from_static(s);
@ -116,18 +113,17 @@ impl Header for HXTarget {
where
I: Iterator<Item = &'i HeaderValue>,
{
let value = values
.next()
.ok_or_else(headers::Error::invalid)?;
let value = values.next().ok_or_else(headers::Error::invalid)?;
Ok(HXTarget{ target: value.clone() })
Ok(HXTarget {
target: value.clone(),
})
}
fn encode<E>(&self, values: &mut E)
where
E: Extend<HeaderValue>,
{
values.extend(std::iter::once(self.target.clone()));
}
}