use async_trait::async_trait;
use reinhardt_di::{DiError, DiResult, Injectable, InjectionContext};
use reinhardt_http::AuthState;
#[derive(Debug, Clone)]
pub struct AuthInfo(pub AuthState);
#[cfg(feature = "params")]
#[async_trait]
impl Injectable for AuthInfo {
async fn inject(ctx: &InjectionContext) -> DiResult<Self> {
let request = ctx.get_http_request().ok_or_else(|| {
DiError::NotFound(
"AuthInfo: No HTTP request available in InjectionContext. \
Ensure the router is configured with .with_di_context()"
.to_string(),
)
})?;
let auth_state: AuthState = request.extensions.get().ok_or_else(|| {
DiError::NotFound(
"AuthInfo: No AuthState found in request extensions. \
Ensure authentication middleware is configured."
.to_string(),
)
})?;
if !auth_state.is_authenticated() {
return Err(DiError::Authentication(
"AuthInfo: User is not authenticated".to_string(),
));
}
Ok(AuthInfo(auth_state))
}
}
#[cfg(not(feature = "params"))]
#[async_trait]
impl Injectable for AuthInfo {
async fn inject(_ctx: &InjectionContext) -> DiResult<Self> {
Err(DiError::NotFound(
"AuthInfo requires the 'params' feature to be enabled".to_string(),
))
}
}