feat: style: return unauthorized when token isnt found and flatten function structure
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful

This commit is contained in:
Radical 2025-05-02 19:26:37 +02:00
parent 1d7cdf343b
commit 0d07af7d79

View file

@ -37,15 +37,23 @@ pub async fn check_access_token(
access_token: String, access_token: String,
pool: &sqlx::Pool<Postgres>, pool: &sqlx::Pool<Postgres>,
) -> Result<Uuid, HttpResponse> { ) -> Result<Uuid, HttpResponse> {
match sqlx::query_as( let row = sqlx::query_as(
"SELECT CAST(uuid as VARCHAR), created FROM access_tokens WHERE token = $1", "SELECT CAST(uuid as VARCHAR), created FROM access_tokens WHERE token = $1",
) )
.bind(&access_token) .bind(&access_token)
.fetch_one(pool) .fetch_one(pool)
.await .await;
{
Ok(row) => { if let Err(error) = row {
let (uuid, created): (String, i64) = row; if error.to_string() == "no rows returned by a query that expected to return at least one row" {
return Err(HttpResponse::Unauthorized().finish())
}
error!("{}", error);
return Err(HttpResponse::InternalServerError().json(r#"{ "error": "Unhandled exception occured, contact the server administrator" }"#))
}
let (uuid, created): (String, i64) = row.unwrap();
let current_time = SystemTime::now() let current_time = SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
@ -60,9 +68,3 @@ pub async fn check_access_token(
Ok(Uuid::from_str(&uuid).unwrap()) Ok(Uuid::from_str(&uuid).unwrap())
} }
Err(error) => {
error!("{}", error);
Err(HttpResponse::InternalServerError().finish())
}
}
}