From eedc536c23c3230c6ebeac8b495b2cfef0317830 Mon Sep 17 00:00:00 2001
From: Santo Cariotti <santo@dcariotti.me>
Date: Tue, 23 Aug 2022 18:10:18 +0200
Subject: Auth login

---
 server/src/routes/auth.rs | 22 ++++++++++++++++++++++
 server/src/routes/mod.rs  |  1 +
 2 files changed, 23 insertions(+)
 create mode 100644 server/src/routes/auth.rs

(limited to 'server/src/routes')

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;
-- 
cgit v1.2.3-18-g5258