pre-metrics

This commit is contained in:
2026-05-15 19:35:06 +02:00
parent 0b441b0759
commit 132057217d
43 changed files with 1170 additions and 170 deletions
+57
View File
@@ -0,0 +1,57 @@
use super::dto::{CheckResponse, LoginRequest, LoginResponse};
use crate::auth::token::create_jwt;
use crate::core::AppState;
use crate::http::context::CurrentUser;
use crate::http::error::HTTPError;
use axum::extract::State;
use axum::Json;
#[utoipa::path(
post,
path = "/api/auth/login",
responses(
(status = 200, description = "Login successful", body = LoginResponse),
(status = 401, description = "Unauthorized")
)
)]
pub async fn login_user_pw(
State(state): State<AppState>,
Json(payload): Json<LoginRequest>,
) -> Result<Json<LoginResponse>, HTTPError> {
let user = state
.repositories
.user
.check_password(&payload.username, &payload.password)
.await
.map_err(|_| HTTPError::Unauthorized)?;
let token = create_jwt(
user.id,
&user.username,
&state.config.jwt.secret,
state.config.jwt.duration,
)
.map_err(|_| HTTPError::InternalServerError("Failed to create JWT token".to_string()))?;
Ok(Json(LoginResponse {
username: user.username,
token,
}))
}
#[utoipa::path(
post,
path = "/api/auth/check",
responses(
(status = 200, description = "Login successful", body = LoginResponse),
(status = 401, description = "Unauthorized")
)
)]
pub async fn check(
State(state): State<AppState>,
user: CurrentUser,
) -> Result<Json<CheckResponse>, HTTPError> {
Ok(Json(CheckResponse {
authenticated: true,
}))
}