reinhardt_auth/
auth_info.rs1use async_trait::async_trait;
7use reinhardt_di::{DiError, DiResult, Injectable, InjectionContext};
8use reinhardt_http::AuthState;
9
10#[derive(Debug, Clone)]
39pub struct AuthInfo(pub AuthState);
40
41#[cfg(feature = "params")]
42#[async_trait]
43impl Injectable for AuthInfo {
44 async fn inject(ctx: &InjectionContext) -> DiResult<Self> {
45 let request = ctx.get_http_request().ok_or_else(|| {
46 DiError::NotFound(
47 "AuthInfo: No HTTP request available in InjectionContext. \
48 Ensure the router is configured with .with_di_context()"
49 .to_string(),
50 )
51 })?;
52
53 let auth_state: AuthState = request.extensions.get().ok_or_else(|| {
54 DiError::NotFound(
55 "AuthInfo: No AuthState found in request extensions. \
56 Ensure authentication middleware is configured."
57 .to_string(),
58 )
59 })?;
60
61 if !auth_state.is_authenticated() {
62 return Err(DiError::NotFound(
63 "AuthInfo: User is not authenticated".to_string(),
64 ));
65 }
66
67 Ok(AuthInfo(auth_state))
68 }
69}
70
71#[cfg(not(feature = "params"))]
72#[async_trait]
73impl Injectable for AuthInfo {
74 async fn inject(_ctx: &InjectionContext) -> DiResult<Self> {
75 Err(DiError::NotFound(
76 "AuthInfo requires the 'params' feature to be enabled".to_string(),
77 ))
78 }
79}