Skip to content

Instantly share code, notes, and snippets.

@mike820324
Created October 4, 2021 13:30
Show Gist options
  • Save mike820324/30da6c51032bb1e7631286288394d1cd to your computer and use it in GitHub Desktop.
Save mike820324/30da6c51032bb1e7631286288394d1cd to your computer and use it in GitHub Desktop.
pub async fn authentication_check(db_conn: &DbPool, request: &HttpRequest) -> Result<(Identity, String, i64)> {
let token = request.cookie("token").ok_or(Error::msg("Cookie Not Found"))?;
let (identity, expireat) = decode_jwt_session(token.value())?;
let db_result: Vec<(Uuid, )> = sqlx::query_as(
r#"
SELECT
id
FROM
blacklisttoken
WHERE
token=$1
"#
)
.bind(token.value())
.fetch_all(&*db_conn)
.await?;
if db_result.len() > 0 {
return Err(Error::msg("Token logout"));
}
Ok((identity, token.value().into(), expireat))
}
pub async fn resource_update(
request: HttpRequest,
app_store: web::Data<DbPool>,
user_data: web::Json<ResourceUpdateRequestData>,
id: web::Path<String>,
) -> impl Responder {
let identity = match authentication_check(&app_store, &request).await {
Ok((identity, _, _)) => identity,
Err(err) => {
return HttpResponse::Unauthorized().finish();
}
};
if identity.role != Role::Admin {
return HttpResponse::Unauthorized().finish();
}
update_resource_by_id(
app_store.get_ref(),
&id,
&user_data.username,
&user_data.email,
)
.await
.unwrap();
HttpResponse::Created().finish()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment