summaryrefslogtreecommitdiff
path: root/server/src/routes
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-08-23 18:10:18 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-08-23 18:10:18 +0200
commiteedc536c23c3230c6ebeac8b495b2cfef0317830 (patch)
tree198007257cfa195c410c28b0a576f46fb8870ab9 /server/src/routes
parent0dace9fb82953cc91e6d603c4c9970631ac036ad (diff)
Auth login
Diffstat (limited to 'server/src/routes')
-rw-r--r--server/src/routes/auth.rs22
-rw-r--r--server/src/routes/mod.rs1
2 files changed, 23 insertions, 0 deletions
diff --git a/server/src/routes/auth.rs b/server/src/routes/auth.rs
new file mode 100644
index 0000000..629ed33
--- /dev/null
+++ b/server/src/routes/auth.rs
@@ -0,0 +1,22 @@
+use crate::errors::AppError;
+use crate::models::{
+ auth::{AuthBody, Claims},
+ user::{User, UserCreate},
+};
+use axum::{routing::post, Json, Router};
+
+pub fn create_route() -> Router {
+ Router::new().route("/login", post(make_login))
+}
+
+async fn make_login(Json(payload): Json<UserCreate>) -> Result<Json<AuthBody>, AppError> {
+ let user = User::new(payload.email, payload.password);
+ match User::find(user).await {
+ Ok(user) => {
+ let claims = Claims::new(user.id);
+ let token = claims.get_token()?;
+ Ok(Json(AuthBody::new(token)))
+ }
+ Err(_) => Err(AppError::NotFound),
+ }
+}
diff --git a/server/src/routes/mod.rs b/server/src/routes/mod.rs
index 22d12a3..f9bae3d 100644
--- a/server/src/routes/mod.rs
+++ b/server/src/routes/mod.rs
@@ -1 +1,2 @@
+pub mod auth;
pub mod user;