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
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
This commit is contained in:
parent
1d7cdf343b
commit
0d07af7d79
1 changed files with 23 additions and 21 deletions
|
@ -37,15 +37,23 @@ pub async fn check_access_token(
|
|||
access_token: String,
|
||||
pool: &sqlx::Pool<Postgres>,
|
||||
) -> Result<Uuid, HttpResponse> {
|
||||
match sqlx::query_as(
|
||||
let row = sqlx::query_as(
|
||||
"SELECT CAST(uuid as VARCHAR), created FROM access_tokens WHERE token = $1",
|
||||
)
|
||||
.bind(&access_token)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
{
|
||||
Ok(row) => {
|
||||
let (uuid, created): (String, i64) = row;
|
||||
.await;
|
||||
|
||||
if let Err(error) = 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()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
|
@ -59,10 +67,4 @@ pub async fn check_access_token(
|
|||
}
|
||||
|
||||
Ok(Uuid::from_str(&uuid).unwrap())
|
||||
}
|
||||
Err(error) => {
|
||||
error!("{}", error);
|
||||
Err(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue