summaryrefslogtreecommitdiffstats
path: root/src/routes/auth.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-01 16:45:04 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-01 16:45:04 +0000
commitab23761e090b8ab6311a360eada7131f6663a3bf (patch)
treeb5a99bb4cfc811e45fc2e3680b4f8b1e944515eb /src/routes/auth.rs
Fork from m6-ie project
Diffstat (limited to 'src/routes/auth.rs')
-rw-r--r--src/routes/auth.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/routes/auth.rs b/src/routes/auth.rs
new file mode 100644
index 0000000..37c41b2
--- /dev/null
+++ b/src/routes/auth.rs
@@ -0,0 +1,25 @@
+use crate::errors::AppError;
+use crate::models::{
+ auth::{AuthBody, Claims},
+ user::{User, UserCreate},
+};
+use axum::{routing::post, Json, Router};
+
+/// Create routes for `/v1/auth/` namespace
+pub fn create_route() -> Router {
+ Router::new().route("/login", post(make_login))
+}
+
+/// Make login. Check if a user with the email and password passed in request body exists into the
+/// database
+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),
+ }
+}