diff --git a/src/auth.rs b/src/auth.rs index 5be38d3..c01d7da 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,4 +1,6 @@ +use axum::response::{IntoResponse, Response}; use axum_login::{AuthUser, AuthnBackend, UserId}; +use http::StatusCode; use julid::Julid; use password_auth::verify_password; use sqlx::SqlitePool; @@ -88,3 +90,16 @@ pub async fn session_layer(pool: SqlitePool) -> SessionManagerLayer .with_secure(false) .with_expiry(Expiry::OnInactivity(SESSION_TTL)) } + +impl IntoResponse for AuthError { + fn into_response(self) -> Response { + match self.0 { + AuthErrorKind::Internal => ( + StatusCode::INTERNAL_SERVER_ERROR, + "An unknown error occurred; you cursed, brah?", + ) + .into_response(), + AuthErrorKind::Unknown => (StatusCode::OK, "Not successful.").into_response(), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 295210a..f64c07e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use axum::{ body::Body, middleware, - response::IntoResponse, + response::{IntoResponse, Response}, routing::{get, post, IntoMakeService}, }; use axum_login::AuthManagerLayerBuilder; @@ -116,16 +116,16 @@ pub async fn app(db_pool: sqlx::SqlitePool) -> IntoMakeService { //-************************************************************************ pub(crate) trait Wender { - fn wender(self) -> Body; + fn wender(self) -> Response; } impl Wender for askama::Result { - fn wender(self) -> Body { + fn wender(self) -> Response { let b = self.unwrap_or_else(|e| { tracing::error!("got error rendering template: {e}"); "".to_string() }); - Body::new(b) + Body::new(b).into_response() } } diff --git a/src/login.rs b/src/login.rs index dcf14a4..bfad581 100644 --- a/src/login.rs +++ b/src/login.rs @@ -1,7 +1,6 @@ use askama::Template; use axum::{ - http::StatusCode, - response::{IntoResponse, Redirect, Response}, + response::{IntoResponse, Redirect}, Form, }; use serde::{Deserialize, Serialize}; @@ -15,19 +14,6 @@ use crate::{ // Login error and success types //-************************************************************************ -impl IntoResponse for AuthError { - fn into_response(self) -> Response { - match self.0 { - AuthErrorKind::Internal => ( - StatusCode::INTERNAL_SERVER_ERROR, - "An unknown error occurred; you cursed, brah?", - ) - .into_response(), - AuthErrorKind::Unknown => (StatusCode::OK, "Not successful.").into_response(), - } - } -} - // for receiving form submissions #[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] pub struct LoginPostForm { @@ -58,7 +44,7 @@ pub async fn post_login( let dest = login_form.destination.take(); let user = match auth.authenticate(login_form.clone().into()).await { Ok(Some(user)) => user, - Ok(None) => return Ok(LoginPage::default().render().wender().into_response()), + Ok(None) => return Ok(LoginPage::default().render().wender()), Err(_) => { let err: AuthError = AuthErrorKind::Internal.into(); return Err(err.into()); @@ -87,7 +73,7 @@ pub async fn get_logout() -> impl IntoResponse { pub async fn post_logout(mut auth: AuthSession) -> impl IntoResponse { match auth.logout().await { - Ok(_) => LogoutSuccessPage.render().wender().into_response(), + Ok(_) => LogoutSuccessPage.render().wender(), Err(e) => { tracing::debug!("{e}"); let e: AuthError = AuthErrorKind::Internal.into(); diff --git a/src/signup/handlers.rs b/src/signup/handlers.rs index 8ea9b4a..ec2458b 100644 --- a/src/signup/handlers.rs +++ b/src/signup/handlers.rs @@ -54,8 +54,7 @@ pub async fn get_create_user( ..Default::default() } .render() - .wender() - .into_response()) + .wender()) } /// Post Handler: validates form values and calls the actual, private user @@ -127,10 +126,7 @@ pub async fn get_signup_success( .unwrap_or_default() }; - let mut resp = SignupSuccessPage(user.clone()) - .render() - .wender() - .into_response(); + let mut resp = SignupSuccessPage(user.clone()).render().wender(); if user.username.is_empty() || id.is_alpha() { // redirect to front page if we got here without a valid user ID diff --git a/src/watches/handlers.rs b/src/watches/handlers.rs index f57ae66..0de7a57 100644 --- a/src/watches/handlers.rs +++ b/src/watches/handlers.rs @@ -286,7 +286,6 @@ pub async fn get_watch( } .render() .wender() - .into_response() } /// everything the user has saved @@ -302,10 +301,7 @@ pub async fn get_watches(auth: AuthSession, State(pool): State) -> i vec![] }; - MyWatchesPage { watches, user } - .render() - .wender() - .into_response() + MyWatchesPage { watches, user }.render().wender() } pub async fn get_watch_status( @@ -323,10 +319,7 @@ pub async fn get_watch_status( .map_err(|e| { tracing::error!("Got error from checking watch status: {e:?}"); })?; - Ok(WatchStatusMenus { watch, quest } - .render() - .wender() - .into_response()) + Ok(WatchStatusMenus { watch, quest }.render().wender()) } else { Ok("Login to add".into_response()) }