This repository has been archived on 2024-08-23. You can view files and clone it, but cannot push or open issues or pull requests.
project/src/auth.rs

27 lines
660 B
Rust
Raw Normal View History

2024-06-27 12:04:57 +00:00
use axum::{
extract::{FromRequestParts, Query},
http::{request::Parts, StatusCode},
RequestPartsExt,
};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Claims {
pub user_id: i32,
}
#[axum::async_trait]
2024-08-01 17:30:10 +00:00
impl<T> FromRequestParts<T> for Claims {
2024-06-27 12:04:57 +00:00
type Rejection = StatusCode;
2024-08-01 17:30:10 +00:00
async fn from_request_parts(parts: &mut Parts, _state: &T) -> Result<Self, Self::Rejection> {
2024-06-27 12:04:57 +00:00
match parts.extract().await {
Ok(Query(claims)) => Ok(claims),
Err(err) => {
tracing::debug!(%err, "Autharization failed");
Err(StatusCode::UNAUTHORIZED)
}
}
}
}