Skip to main content

architect_sdk/extractors/
user.rs

1//! Extract user id from request (X-User-ID header).
2
3use async_trait::async_trait;
4use axum::{extract::FromRequestParts, http::request::Parts};
5
6pub const USER_ID_HEADER: &str = "X-User-ID";
7
8#[derive(Clone, Debug)]
9pub struct UserId(pub Option<String>);
10
11#[async_trait]
12impl<S> FromRequestParts<S> for UserId
13where
14    S: Send + Sync,
15{
16    type Rejection = std::convert::Infallible;
17
18    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
19        let value = parts
20            .headers
21            .get(USER_ID_HEADER)
22            .and_then(|v| v.to_str().ok())
23            .map(|s| s.trim().to_string())
24            .filter(|s| !s.is_empty());
25        Ok(UserId(value))
26    }
27}