main
use actix_web::body::{BoxBody, MessageBody};
use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::middleware::Next;
use actix_web::{Error, HttpMessage};
use crate::error::SessionError;
use crate::state::Session;
pub async fn session_guard<B>(
request: ServiceRequest,
next: Next<B>,
) -> Result<ServiceResponse<BoxBody>, Error>
where
B: MessageBody + 'static,
{
let authorized = request
.extensions()
.get::<Session>()
.and_then(|session| session.uid())
.is_some();
if !authorized {
let request_id = request
.extensions()
.get::<envelope::RequestId>()
.map(|value| value.0.clone())
.unwrap_or_default();
let response = SessionError::Unauthorized.response(&request_id);
return Ok(request.into_response(response));
}
Ok(next.call(request).await?.map_into_boxed_body())
}