diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2022-09-03 10:10:20 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2022-09-03 10:10:20 +0000 |
| commit | 2c6434e0b89e93ab6bdddb28bcd059b48638cb0d (patch) | |
| tree | 34844dc8d86780d25261c9082cf715226696c6ab /src/routes | |
| parent | 6cda1b704d1c38fc116e59a2a206c2adebfb6d4d (diff) | |
Users has username and use it for login
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/auth.rs | 10 | ||||
| -rw-r--r-- | src/routes/user.rs | 4 |
2 files changed, 7 insertions, 7 deletions
diff --git a/src/routes/auth.rs b/src/routes/auth.rs index 37c41b2..b667b97 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -1,7 +1,7 @@ use crate::errors::AppError; use crate::models::{ - auth::{AuthBody, Claims}, - user::{User, UserCreate}, + auth::{AuthBody, Claims, LoginCredentials}, + user::User, }; use axum::{routing::post, Json, Router}; @@ -12,14 +12,14 @@ pub fn create_route() -> Router { /// 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); +async fn make_login(Json(payload): Json<LoginCredentials>) -> Result<Json<AuthBody>, AppError> { + let user = User::new(String::new(), payload.username, 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), + Err(_) => Err(AppError::NotFound("User not found".to_string())), } } diff --git a/src/routes/user.rs b/src/routes/user.rs index d44df66..3dfb73a 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -24,7 +24,7 @@ async fn create_user( Json(payload): Json<UserCreate>, _: Claims, ) -> Result<Json<UserList>, AppError> { - let user = User::new(payload.email, payload.password); + let user = User::new(payload.email, payload.username, payload.password); let user_new = User::create(user).await?; Ok(Json(user_new)) @@ -34,6 +34,6 @@ async fn create_user( async fn get_user(Path(user_id): Path<i32>, _: Claims) -> Result<Json<UserList>, AppError> { match User::find_by_id(user_id).await { Ok(user) => Ok(Json(user)), - Err(_) => Err(AppError::NotFound), + Err(_) => Err(AppError::NotFound("User not found".to_string())), } } |
