This commit is contained in:
2026-06-09 23:05:35 +02:00
parent ee2fc42fff
commit eb2652f7e9
27 changed files with 665 additions and 538 deletions
+16 -13
View File
@@ -1,14 +1,17 @@
use super::dto::{CheckResponse, LoginRequest, LoginResponse};
use super::dto::{LoginRequest, LoginResponse, MeResponse};
use crate::auth::token::create_jwt;
use crate::core::AppState;
use crate::http::context::CurrentUser;
use crate::http::error::HTTPError;
use crate::routes::user::mapper::user_model_to_user_response;
use axum::extract::State;
use axum::Json;
use sea_orm::ActiveModelBehavior;
#[utoipa::path(
post,
get,
path = "/auth/login",
request_body = LoginRequest,
responses(
(status = 200, description = "Login successful", body = LoginResponse),
(status = 401, description = "Unauthorized")
@@ -29,22 +32,20 @@ pub async fn login_user_pw(
let token = create_jwt(
user.id,
&user.username,
user.is_superuser,
&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,
}))
Ok(Json(LoginResponse { token }))
}
#[utoipa::path(
post,
path = "/auth/check",
path = "/auth/me",
responses(
(status = 200, description = "Login successful", body = LoginResponse),
(status = 200, description = "Token valid", body = LoginResponse),
(status = 401, description = "Unauthorized")
),
security(
@@ -52,11 +53,13 @@ pub async fn login_user_pw(
),
tag = "Auth"
)]
pub async fn check(
pub async fn me(
State(_state): State<AppState>,
_user: CurrentUser,
) -> Result<Json<CheckResponse>, HTTPError> {
Ok(Json(CheckResponse {
authenticated: true,
CurrentUser(user): CurrentUser,
) -> Result<Json<MeResponse>, HTTPError> {
let user_response = user_model_to_user_response(user);
Ok(Json(MeResponse {
user: user_response,
}))
}