summaryrefslogtreecommitdiff
path: root/src/user/routes/login/post.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2025-04-03 23:45:23 -0400
committerOwen Jacobson <owen@grimoire.ca>2025-04-03 23:45:23 -0400
commit9f7f82dbd9adee8ae18ae7ff2600b3e1dc8fadbc (patch)
treed973d00486ffab3445e3ca454e93a941ed8fe6e2 /src/user/routes/login/post.rs
parent24755a89a97a4d1cb10ebbcf41e200861f3bedf3 (diff)
parent45eea07a56022f647b3a273798a5255cda73f13d (diff)
Merge branch 'prop/rename-login-to-user'
Diffstat (limited to 'src/user/routes/login/post.rs')
-rw-r--r--src/user/routes/login/post.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/user/routes/login/post.rs b/src/user/routes/login/post.rs
new file mode 100644
index 0000000..39f9eea
--- /dev/null
+++ b/src/user/routes/login/post.rs
@@ -0,0 +1,52 @@
+use axum::{
+ extract::{Json, State},
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+
+use crate::{
+ app::App,
+ clock::RequestedAt,
+ error::Internal,
+ name::Name,
+ token::{app, extract::IdentityCookie},
+ user::{Password, User},
+};
+
+pub async fn handler(
+ State(app): State<App>,
+ RequestedAt(now): RequestedAt,
+ identity: IdentityCookie,
+ Json(request): Json<Request>,
+) -> Result<(IdentityCookie, Json<User>), Error> {
+ let (user, secret) = app
+ .tokens()
+ .login(&request.name, &request.password, &now)
+ .await
+ .map_err(Error)?;
+ let identity = identity.set(secret);
+ Ok((identity, Json(user)))
+}
+
+#[derive(serde::Deserialize)]
+pub struct Request {
+ pub name: Name,
+ pub password: Password,
+}
+
+#[derive(Debug, thiserror::Error)]
+#[error(transparent)]
+pub struct Error(#[from] pub app::LoginError);
+
+impl IntoResponse for Error {
+ fn into_response(self) -> Response {
+ let Self(error) = self;
+ match error {
+ app::LoginError::Rejected => {
+ // not error::Unauthorized due to differing messaging
+ (StatusCode::UNAUTHORIZED, "invalid name or password").into_response()
+ }
+ other => Internal::from(other).into_response(),
+ }
+ }
+}