-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
3607dc9 refactor: [#446] fixed text formatting (Mario) 4208f3a refactor: [#446] refactored handlers with new extractor (Mario) 8f7b8a6 feat: [#446] new optional user id extractor (Mario) Pull request description: Resolves #446. ACKs for top commit: josecelano: ACK 3607dc9 Tree-SHA512: 26dc989751076f094344725e6f6cd8efa51164d0a4160aa0e492e4ec3bc27b71c62dbf81db7ff9dc256f058dfc1fc4580b61f0e332da0f7d9d0041486c39cd4e
- Loading branch information
Showing
3 changed files
with
45 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod bearer_token; | ||
pub mod optional_user_id; | ||
pub mod user_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use axum::extract::{FromRef, FromRequestParts}; | ||
use axum::http::request::Parts; | ||
use axum::response::Response; | ||
|
||
use crate::common::AppData; | ||
use crate::models::user::UserId; | ||
use crate::web::api::server::v1::extractors::bearer_token; | ||
|
||
pub struct ExtractOptionalLoggedInUser(pub Option<UserId>); | ||
|
||
#[async_trait] | ||
impl<S> FromRequestParts<S> for ExtractOptionalLoggedInUser | ||
where | ||
Arc<AppData>: FromRef<S>, | ||
S: Send + Sync, | ||
{ | ||
type Rejection = Response; | ||
|
||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
/* let maybe_bearer_token = match bearer_token::Extract::from_request_parts(parts, state).await { | ||
Ok(maybe_bearer_token) => maybe_bearer_token.0, | ||
Err(_) => return Err(ServiceError::TokenNotFound.into_response()), | ||
}; */ | ||
|
||
let bearer_token = match bearer_token::Extract::from_request_parts(parts, state).await { | ||
Ok(bearer_token) => bearer_token.0, | ||
Err(_) => None, | ||
}; | ||
|
||
//Extracts the app state | ||
let app_data = Arc::from_ref(state); | ||
|
||
match app_data.auth.get_user_id_from_bearer_token(&bearer_token).await { | ||
Ok(user_id) => Ok(ExtractOptionalLoggedInUser(Some(user_id))), | ||
Err(_) => Ok(ExtractOptionalLoggedInUser(None)), | ||
} | ||
} | ||
} |