pre-metrics
This commit is contained in:
@@ -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,
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user