Add middleware to check authentication

This commit is contained in:
2023-09-02 19:15:11 +02:00
parent 849bf0cdfb
commit caaf3d703f
7 changed files with 126 additions and 8 deletions

View File

@ -1,5 +1,5 @@
use crate::app_config::AppConfig;
use crate::extractors::auth_extractor::AuthChecker;
use crate::extractors::auth_extractor::AuthExtractor;
use crate::extractors::local_auth_extractor::LocalAuthEnabled;
use actix_web::{web, HttpResponse, Responder};
@ -13,7 +13,7 @@ pub struct LocalAuthReq {
pub async fn local_auth(
local_auth_enabled: LocalAuthEnabled,
req: web::Json<LocalAuthReq>,
auth: AuthChecker,
auth: AuthExtractor,
) -> impl Responder {
if !*local_auth_enabled {
log::error!("Local auth attempt while this authentication method is disabled!");
@ -29,3 +29,15 @@ pub async fn local_auth(
HttpResponse::Accepted().json("Welcome")
}
#[derive(serde::Serialize)]
struct CurrentUser {
id: String,
}
/// Get current authenticated user
pub async fn current_user(auth: AuthExtractor) -> impl Responder {
HttpResponse::Ok().json(CurrentUser {
id: auth.id().unwrap(),
})
}