diff options
Diffstat (limited to 'server/src/routes')
-rw-r--r-- | server/src/routes/auth.rs | 22 | ||||
-rw-r--r-- | server/src/routes/mod.rs | 1 |
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; |