Dmitri 8b69b59485
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 13m48s
fixed old todos
2026-05-04 23:49:33 +02:00

26 lines
835 B
Rust

use crate::{errors::ApiError, state::AppState, utils::jwt::verify_access_token};
use axum::{
extract::{Request, State},
middleware::Next,
response::Response,
};
pub async fn auth_middleware(
State(state): State<AppState>,
mut request: Request,
next: Next,
) -> Result<Response, ApiError> {
let auth_header = request
.headers()
.get(axum::http::header::AUTHORIZATION)
.and_then(|h| h.to_str().ok())
.ok_or(ApiError::Unauthorized)?;
if !auth_header.starts_with("Bearer ") {
return Err(ApiError::Unauthorized);
}
let token = &auth_header[7..];
let claims = verify_access_token(token, &state.jwt_secret)?;
// Inject the user ID into extensions for downstream handlers
request.extensions_mut().insert(claims.sub);
Ok(next.run(request).await)
}