Skip to content

Commit

Permalink
Fix: Refactor auth function
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwd committed Jun 9, 2024
1 parent 24b4885 commit d755cbb
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/utils/session.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::{http::StatusCode, response::IntoResponse};
use uuid::Uuid;
use crate::{config::database, models::user::User};

Expand All @@ -20,23 +21,19 @@ pub async fn create(user_id: Uuid) -> Uuid{
}
}

pub async fn authenticate(session_id: Uuid) -> User{
pub async fn authenticate(session_id: Uuid) -> Result<User, impl IntoResponse> {
let db_pool = database::connection().await;

let query_result = sqlx::query_as!(
User,
"SELECT name, email, id FROM users WHERE users.id = (SELECT user_id FROM sessions WHERE id = ($1))",
"SELECT name, email, id FROM users WHERE users.id = (SELECT user_id FROM sessions WHERE id = $1)",
session_id
)
.fetch_one(&db_pool)
.await;

match query_result {
Ok(user) => {
return user;
}
Err(e) => {
panic!("Failed to authenticate session. {:?}", e);
}
Ok(user) => Ok(user),
Err(e) => Err((StatusCode::UNAUTHORIZED, format!("Failed to authenticate session: {:?}", e))),
}
}

0 comments on commit d755cbb

Please sign in to comment.