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, Json(body): Json, ) -> Result { 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, Json(body): Json, ) -> Result { 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), })) }