Error handling refactor and 500 page/message templates
This commit is contained in:
@@ -4,7 +4,12 @@ mod logout;
|
||||
mod newsletters;
|
||||
mod posts;
|
||||
|
||||
use crate::routes::error_chain_fmt;
|
||||
use crate::{
|
||||
authentication::AuthenticatedUser,
|
||||
routes::{AppError, error_chain_fmt},
|
||||
session_state::TypedSession,
|
||||
};
|
||||
use axum::{extract::Request, middleware::Next, response::Response};
|
||||
pub use change_password::*;
|
||||
pub use dashboard::*;
|
||||
pub use logout::*;
|
||||
@@ -30,3 +35,28 @@ impl std::fmt::Debug for AdminError {
|
||||
error_chain_fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn require_auth(
|
||||
session: TypedSession,
|
||||
mut request: Request,
|
||||
next: Next,
|
||||
) -> Result<Response, AppError> {
|
||||
let user_id = session
|
||||
.get_user_id()
|
||||
.await
|
||||
.map_err(|e| AdminError::UnexpectedError(e.into()))?
|
||||
.ok_or(AdminError::NotAuthenticated)?;
|
||||
let username = session
|
||||
.get_username()
|
||||
.await
|
||||
.map_err(|e| AdminError::UnexpectedError(e.into()))?
|
||||
.ok_or(AdminError::UnexpectedError(anyhow::anyhow!(
|
||||
"Could not find username in session."
|
||||
)))?;
|
||||
|
||||
request
|
||||
.extensions_mut()
|
||||
.insert(AuthenticatedUser { user_id, username });
|
||||
|
||||
Ok(next.run(request).await)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user