Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m15s
test / unit (pull_request) Successful in 16m3s
test / integration (pull_request) Failing after 17m19s
83 lines
2.4 KiB
Rust
83 lines
2.4 KiB
Rust
use crate::{errors::ApiError, state::AppState};
|
|
use api_types::{
|
|
requests::{LoginRequest, RegisterRequest},
|
|
responses::{AuthResponse, ErrorResponse, UserResponse},
|
|
};
|
|
use application::use_cases::auth::{login, register, LoginInput, RegisterInput};
|
|
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
|
|
|
|
pub fn to_user_response(u: &domain::models::user::User) -> UserResponse {
|
|
UserResponse {
|
|
id: u.id.as_uuid(),
|
|
username: u.username.to_string(),
|
|
display_name: u.display_name.clone(),
|
|
bio: u.bio.clone(),
|
|
avatar_url: u.avatar_url.clone(),
|
|
header_url: u.header_url.clone(),
|
|
custom_css: u.custom_css.clone(),
|
|
local: u.local,
|
|
is_followed_by_viewer: false,
|
|
created_at: u.created_at,
|
|
}
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post, path = "/auth/register",
|
|
request_body = RegisterRequest,
|
|
responses(
|
|
(status = 201, description = "User registered", body = AuthResponse),
|
|
(status = 409, description = "Username or email taken", body = ErrorResponse),
|
|
(status = 422, description = "Invalid input", body = ErrorResponse),
|
|
)
|
|
)]
|
|
pub async fn post_register(
|
|
State(s): State<AppState>,
|
|
Json(body): Json<RegisterRequest>,
|
|
) -> Result<impl IntoResponse, ApiError> {
|
|
let out = register(
|
|
&*s.users,
|
|
&*s.hasher,
|
|
&*s.auth,
|
|
&*s.events,
|
|
RegisterInput {
|
|
username: body.username,
|
|
email: body.email,
|
|
password: body.password,
|
|
},
|
|
)
|
|
.await?;
|
|
let resp = AuthResponse {
|
|
token: out.token,
|
|
user: to_user_response(&out.user),
|
|
};
|
|
Ok((StatusCode::CREATED, Json(resp)))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post, path = "/auth/login",
|
|
request_body = LoginRequest,
|
|
responses(
|
|
(status = 200, description = "Login successful", body = AuthResponse),
|
|
(status = 401, description = "Invalid credentials", body = ErrorResponse),
|
|
)
|
|
)]
|
|
pub async fn post_login(
|
|
State(s): State<AppState>,
|
|
Json(body): Json<LoginRequest>,
|
|
) -> Result<impl IntoResponse, ApiError> {
|
|
let out = login(
|
|
&*s.users,
|
|
&*s.hasher,
|
|
&*s.auth,
|
|
LoginInput {
|
|
email: body.email,
|
|
password: body.password,
|
|
},
|
|
)
|
|
.await?;
|
|
Ok(Json(AuthResponse {
|
|
token: out.token,
|
|
user: to_user_response(&out.user),
|
|
}))
|
|
}
|