use axum::{ extract::{Json, State}, http::StatusCode, response::{IntoResponse, Response}, }; use crate::{ app::App, clock::RequestedAt, error::Internal, login::Password, setup::app, token::extract::IdentityToken, }; pub async fn handler( State(app): State, RequestedAt(setup_at): RequestedAt, identity: IdentityToken, Json(request): Json, ) -> Result<(IdentityToken, StatusCode), Error> { let secret = app .setup() .initial(&request.name, &request.password, &setup_at) .await .map_err(Error)?; let identity = identity.set(secret); Ok((identity, StatusCode::NO_CONTENT)) } #[derive(serde::Deserialize)] pub struct Request { pub name: String, pub password: Password, } #[derive(Debug)] pub struct Error(pub app::Error); impl IntoResponse for Error { fn into_response(self) -> Response { let Self(error) = self; match error { app::Error::SetupCompleted => (StatusCode::CONFLICT, error.to_string()).into_response(), other => Internal::from(other).into_response(), } } }