Skip to content

Commit

Permalink
Chore: Apply formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwd committed Apr 27, 2024
1 parent 071cda8 commit d45c36e
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
models::user::{ CheckUserLogin, CreateUser, LoginUser, User},
structs::app_state::AppState, utils::session,
models::user::{CheckUserLogin, CreateUser, LoginUser, User},
structs::app_state::AppState,
utils::session,
};
use axum::{
extract::{Path, State},
Expand All @@ -15,9 +16,13 @@ pub async fn get_one(
State(app_state): State<Arc<AppState>>,
Path(id): Path<uuid::Uuid>,
) -> Result<impl IntoResponse, impl IntoResponse> {
let query_result = sqlx::query_as!(User, "SELECT name, email, id FROM users WHERE users.id = $1", id)
.fetch_one(&app_state.db)
.await;
let query_result = sqlx::query_as!(
User,
"SELECT name, email, id FROM users WHERE users.id = $1",
id
)
.fetch_one(&app_state.db)
.await;
match query_result {
Ok(user) => {
let user_response = json!(user);
Expand All @@ -36,21 +41,28 @@ pub async fn authenticate(
State(app_state): State<Arc<AppState>>,
Json(body): Json<LoginUser>,
) -> Result<impl IntoResponse, impl IntoResponse> {
let user = sqlx::query_as!(CheckUserLogin, "SELECT id, email, password_hash FROM users WHERE users.email = $1", body.email)
.fetch_one(&app_state.db)
.await;
let user = sqlx::query_as!(
CheckUserLogin,
"SELECT id, email, password_hash FROM users WHERE users.email = $1",
body.email
)
.fetch_one(&app_state.db)
.await;

match user {
Ok(user) => {
let is_valid = bcrypt::verify(body.password, &user.password_hash).unwrap();
if is_valid {
let _session_id = session::create(user.id).await;
return Ok((StatusCode::OK, Json(json!({"status": "success", "message": "User is authorized"}))));
return Ok((
StatusCode::OK,
Json(json!({"status": "success", "message": "User is authorized"})),
));
} else {
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({"status": "error","message": "Wrong Credentials"})),
))
));
}
}
Err(e) => {
Expand Down

0 comments on commit d45c36e

Please sign in to comment.