Skip to content

Commit

Permalink
fix: Refactor user authentication logic in auth_routes.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ikurotime committed Jan 5, 2024
1 parent 6a01348 commit 0a7bff5
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/routes/auth_routes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
http::ApiContext,
structs::{LoginRequest, LoginResponse, RegisterRequest, User},
structs::{LoginRequest, LoginResponse, RegisterRequest},
};

use axum::{
Expand All @@ -11,7 +11,7 @@ use axum::{
Extension, Form, Json, Router,
};
use bcrypt::DEFAULT_COST;
use sqlx::{Error, PgPool};
use sqlx::{Error, PgPool, Row};

pub fn get_routes() -> Router {
Router::new()
Expand All @@ -34,29 +34,18 @@ async fn login(
}

async fn get_user(username: &str, password: &str, pool: &PgPool) -> Result<LoginResponse, Error> {
let user = sqlx::query_as!(
User,
"SELECT id, name, email, password FROM users WHERE name = $1",
username,
)
.fetch_one(pool)
.await
.map_err(|e| match e {
sqlx::Error::RowNotFound => Error::RowNotFound,
_ => e.into(),
})?;

let pswd = user.password.ok_or(Error::RowNotFound)?;
bcrypt::verify(password, &pswd).map_err(|_| Error::RowNotFound)?;
let q = "SELECT id, name, email, password FROM users WHERE name = $1";
let user = sqlx::query(q).bind(&username).fetch_one(pool).await?;
let hashed_password = user.try_get::<String, _>("password")?;

let name = user.name.ok_or(Error::RowNotFound)?;
let email = user.email.ok_or(Error::RowNotFound)?;
bcrypt::verify(password, &hashed_password).map_err(|_| Error::RowNotFound)?;

Ok(LoginResponse {
id: user.id,
name,
email,
})
let response = LoginResponse {
id: user.try_get("id")?,
name: user.try_get("name")?,
email: user.try_get("email")?,
};
Ok(response)
}

async fn register(ctx: Extension<ApiContext>, Form(payload): Form<RegisterRequest>) -> StatusCode {
Expand Down

0 comments on commit 0a7bff5

Please sign in to comment.