feat: implement AuthenticatedRequestInfo for user authentication handling
This commit is contained in:
@@ -1,6 +1,31 @@
|
|||||||
|
use axum::{extract::FromRequestParts, http::StatusCode};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct RequestInfo {
|
pub struct RequestInfo {
|
||||||
pub user_id: Option<Uuid>,
|
pub user_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct AuthenticatedRequestInfo {
|
||||||
|
pub user_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRequestParts<()> for AuthenticatedRequestInfo {
|
||||||
|
type Rejection = StatusCode;
|
||||||
|
|
||||||
|
async fn from_request_parts(
|
||||||
|
parts: &mut axum::http::request::Parts,
|
||||||
|
_state: &(),
|
||||||
|
) -> Result<Self, Self::Rejection> {
|
||||||
|
let request_info = parts
|
||||||
|
.extensions
|
||||||
|
.get::<RequestInfo>()
|
||||||
|
.ok_or(StatusCode::UNAUTHORIZED)?;
|
||||||
|
|
||||||
|
if let Some(user_id) = request_info.user_id {
|
||||||
|
Ok(AuthenticatedRequestInfo { user_id })
|
||||||
|
} else {
|
||||||
|
Err(StatusCode::UNAUTHORIZED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user