Files
gitks/api/response.rs
T
zhenyi 0d3b53f7a0 feat(auth): add comprehensive authentication system with 2FA support
- Add new auth module with captcha, login, logout, register, and email verification endpoints
- Implement two-factor authentication with TOTP enable, disable, verify, and backup codes regeneration
- Create RSA public key endpoint for secure password encryption
- Add user profile management with get current user and email retrieval
- Integrate OpenAPI documentation for all authentication endpoints
- Implement password reset functionality with email verification flow
- Add comprehensive API response structures with proper error handling
- Configure all auth routes under /api/v1/auth scope with proper tagging
2026-06-07 18:09:38 +08:00

66 lines
1.4 KiB
Rust

use serde::Serialize;
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct ApiResponse<T: Serialize> {
/// Business payload returned by the endpoint.
pub data: T,
}
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct ApiEmptyResponse {
/// Human-readable success message.
pub message: String,
}
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct ApiErrorResponse {
/// Stable, client-safe error message.
pub error: String,
}
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct ApiListResponse<T: Serialize> {
pub data: Vec<T>,
pub total: i64,
pub page: i64,
pub per_page: i64,
}
impl<T: Serialize> ApiResponse<T> {
pub fn new(data: T) -> Self {
Self { data }
}
}
impl ApiEmptyResponse {
pub fn ok(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl<T: Serialize> ApiListResponse<T> {
pub fn new(data: Vec<T>, total: i64, page: i64, per_page: i64) -> Self {
Self {
data,
total,
page,
per_page,
}
}
pub fn empty() -> Self {
Self {
data: vec![],
total: 0,
page: 0,
per_page: 0,
}
}
}
impl<T: Serialize> From<(Vec<T>, i64, i64, i64)> for ApiListResponse<T> {
fn from(value: (Vec<T>, i64, i64, i64)) -> Self {
Self::new(value.0, value.1, value.2, value.3)
}
}