From 796fef417667a7d118a9ef0099571df10f861bdf Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 24 Jan 2024 13:37:52 +0100 Subject: [PATCH] refactor: [#39] user id extractor code cleanup --- src/web/api/v1/extractors/user_id.rs | 42 +++------------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/src/web/api/v1/extractors/user_id.rs b/src/web/api/v1/extractors/user_id.rs index 5edb1f1c..71cdad1f 100644 --- a/src/web/api/v1/extractors/user_id.rs +++ b/src/web/api/v1/extractors/user_id.rs @@ -4,24 +4,14 @@ use async_trait::async_trait; use axum::extract::{FromRef, FromRequestParts}; use axum::http::request::Parts; use axum::response::{IntoResponse, Response}; -use serde::Deserialize; use super::bearer_token; use crate::common::AppData; use crate::errors::ServiceError; +use crate::models::user::UserId; pub struct ExtractLoggedInUser(pub UserId); -#[derive(Deserialize, Debug)] -pub struct UserId(i64); - -impl UserId { - #[must_use] - pub fn value(self) -> i64 { - self.0 - } -} - #[async_trait] impl FromRequestParts for ExtractLoggedInUser where @@ -36,36 +26,12 @@ where Err(_) => return Err(ServiceError::Unauthorized.into_response()), }; - /* let app_data = match axum::extract::State::from_request_parts(parts, state).await { - Ok(app_data) => Ok(app_data.0), - Err(_) => Err(ServiceError::Unauthorized), - }; */ - + //Extracts the app state let app_data = Arc::from_ref(state); - /* match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { - Ok(user_id) => ExtractLoggedInUser(UserId(user_id)), - Error(err) => return ServiceError::Unauthorized.into_response(), - } */ - - /* let user_id = ExtractLoggedInUser(UserId(app_data - .auth - .get_user_id_from_bearer_token(&maybe_bearer_token)) - .await - .map_err(|e| { - dbg!(e); - ServiceError::Unauthorized - })? */ - let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await; - - match user_id { - Ok(user_id) => Ok(ExtractLoggedInUser(UserId(user_id))), + match app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await { + Ok(user_id) => Ok(ExtractLoggedInUser(user_id)), Err(error) => Err(error.into_response()), } - - /* match header { - Some(header_value) => Ok(Extract(Some(BearerToken(parse_token(header_value))))), - None => Ok(Extract(None)), - } */ } }