Skip to main content

server_middleware/extract/
auth.rs

1use std::sync::Arc;
2
3use axum::{extract::FromRequestParts, http::request::Parts};
4use server_common::{error::auth::AuthError, jwt::Claims};
5
6pub struct Auth(pub Arc<Claims>);
7
8/// > 定义用户信息提取器,提取用户编号和角色信息
9///
10impl<S> FromRequestParts<S> for Auth
11where
12    S: Send + Sync,
13{
14    type Rejection = AuthError;
15
16    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
17        let claims = parts
18            .extensions
19            .get::<Arc<Claims>>()
20            .ok_or(AuthError::MissingToken)?;
21        // 拼装对象,不使用Claims,减少开销
22        let auth_info = Auth(claims.clone());
23        Ok(auth_info)
24    }
25}