have wender return a response instead of a bare body
This commit is contained in:
parent
59f2acf8a7
commit
1aa2d48869
5 changed files with 26 additions and 36 deletions
15
src/auth.rs
15
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<SqliteStore>
|
|||
.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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<axum::Router> {
|
|||
//-************************************************************************
|
||||
|
||||
pub(crate) trait Wender {
|
||||
fn wender(self) -> Body;
|
||||
fn wender(self) -> Response;
|
||||
}
|
||||
|
||||
impl Wender for askama::Result<String> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
20
src/login.rs
20
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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<SqlitePool>) -> 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("<a href='/login'>Login to add</a>".into_response())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue