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)] #[derive(Deserialize)]
pub struct AddFeed { pub struct AddFeed {
url: String, url: String,
#[serde(default)]
#[serde_as(as = "NoneAsEmptyString")] #[serde_as(as = "NoneAsEmptyString")]
title: Option<String>, title: Option<String>,
#[serde(default)]
#[serde_as(as = "NoneAsEmptyString")] #[serde_as(as = "NoneAsEmptyString")]
description: Option<String>, description: Option<String>,
} }

View File

@ -9,7 +9,7 @@ use tracing::info;
use crate::auth::verify_password; use crate::auth::verify_password;
use crate::error::{Error, Result}; 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::partials::login_form::{login_form, LoginFormProps};
use crate::{ use crate::{
models::user::{AuthContext, User}, models::user::{AuthContext, User},
@ -50,6 +50,7 @@ pub async fn post(
State(pool): State<PgPool>, State(pool): State<PgPool>,
mut auth: AuthContext, mut auth: AuthContext,
hx_target: Option<TypedHeader<HXTarget>>, hx_target: Option<TypedHeader<HXTarget>>,
hx_request: Option<TypedHeader<HXRequest>>,
layout: Layout, layout: Layout,
Form(login): Form<Login>, Form(login): Form<Login>,
) -> Result<Response> { ) -> Result<Response> {
@ -91,5 +92,8 @@ pub async fn post(
auth.login(&user) auth.login(&user)
.await .await
.map_err(|_| Error::InternalServerError)?; .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 headers::{self, Header};
use http::header::{HeaderName, HeaderValue}; use http::header::{HeaderName, HeaderValue};
use http::StatusCode; use http::StatusCode;
@ -19,6 +19,7 @@ pub const HX_TARGET: HeaderName = HeaderName::from_static("hx-target");
pub struct HXRedirect { pub struct HXRedirect {
location: HeaderValue, location: HeaderValue,
reload: bool, reload: bool,
is_htmx_request: bool,
} }
impl HXRedirect { impl HXRedirect {
@ -26,6 +27,7 @@ impl HXRedirect {
Self { Self {
location: HeaderValue::try_from(uri).expect("URI isn't a valid header value"), location: HeaderValue::try_from(uri).expect("URI isn't a valid header value"),
reload: false, reload: false,
is_htmx_request: true,
} }
} }
@ -33,22 +35,23 @@ impl HXRedirect {
self.reload = reload; self.reload = reload;
self self
} }
pub fn is_htmx(mut self, is_htmx: bool) -> Self {
self.is_htmx_request = is_htmx;
self
}
} }
impl IntoResponse for HXRedirect { impl IntoResponse for HXRedirect {
fn into_response(self) -> Response { fn into_response(self) -> Response {
if self.is_htmx_request {
if self.reload { if self.reload {
( (StatusCode::OK, [(HX_REDIRECT, self.location)]).into_response()
StatusCode::OK,
[(HX_REDIRECT, self.location)],
)
.into_response()
} else { } else {
( (StatusCode::OK, [(HX_LOCATION, self.location)]).into_response()
StatusCode::OK, }
[(HX_LOCATION, self.location)], } else {
) Redirect::to(self.location.to_str().unwrap()).into_response()
.into_response()
} }
} }
} }
@ -72,9 +75,7 @@ impl Header for HXRequest {
where where
I: Iterator<Item = &'i HeaderValue>, I: Iterator<Item = &'i HeaderValue>,
{ {
let value = values let value = values.next().ok_or_else(headers::Error::invalid)?;
.next()
.ok_or_else(headers::Error::invalid)?;
if value == "true" { if value == "true" {
Ok(HXRequest(true)) Ok(HXRequest(true))
@ -89,11 +90,7 @@ impl Header for HXRequest {
where where
E: Extend<HeaderValue>, E: Extend<HeaderValue>,
{ {
let s = if self.0 { let s = if self.0 { "true" } else { "false" };
"true"
} else {
"false"
};
let value = HeaderValue::from_static(s); let value = HeaderValue::from_static(s);
@ -116,18 +113,17 @@ impl Header for HXTarget {
where where
I: Iterator<Item = &'i HeaderValue>, I: Iterator<Item = &'i HeaderValue>,
{ {
let value = values let value = values.next().ok_or_else(headers::Error::invalid)?;
.next()
.ok_or_else(headers::Error::invalid)?;
Ok(HXTarget{ target: value.clone() }) Ok(HXTarget {
target: value.clone(),
})
} }
fn encode<E>(&self, values: &mut E) fn encode<E>(&self, values: &mut E)
where where
E: Extend<HeaderValue>, E: Extend<HeaderValue>,
{ {
values.extend(std::iter::once(self.target.clone())); values.extend(std::iter::once(self.target.clone()));
} }
} }