1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
use axum::{
extract::{Json, State},
http::StatusCode,
response::{IntoResponse, Response},
};
use crate::{
app::App,
clock::RequestedAt,
error::Internal,
name::Name,
setup::app,
token::extract::IdentityCookie,
user::{Password, User},
};
pub async fn handler(
State(app): State<App>,
RequestedAt(setup_at): RequestedAt,
identity: IdentityCookie,
Json(request): Json<Request>,
) -> Result<(IdentityCookie, Json<User>), Error> {
let (login, secret) = app
.setup()
.initial(&request.name, &request.password, &setup_at)
.await
.map_err(Error)?;
let identity = identity.set(secret);
Ok((identity, Json(login)))
}
#[derive(serde::Deserialize)]
pub struct Request {
pub name: Name,
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::InvalidName(_) => {
(StatusCode::BAD_REQUEST, error.to_string()).into_response()
}
app::Error::SetupCompleted => (StatusCode::CONFLICT, error.to_string()).into_response(),
other => Internal::from(other).into_response(),
}
}
}
|