Commit e599dbe 1 parent 17f2364 commit e599dbe Copy full SHA for e599dbe
File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 1
1
pub mod bearer_token;
2
+ pub mod user_id;
Original file line number Diff line number Diff line change
1
+ use std:: sync:: Arc ;
2
+
3
+ use async_trait:: async_trait;
4
+ use axum:: extract:: { FromRef , FromRequestParts } ;
5
+ use axum:: http:: request:: Parts ;
6
+ use axum:: response:: { IntoResponse , Response } ;
7
+
8
+ use super :: bearer_token;
9
+ use crate :: common:: AppData ;
10
+ use crate :: errors:: ServiceError ;
11
+ use crate :: models:: user:: UserId ;
12
+
13
+ pub struct ExtractLoggedInUser ( pub UserId ) ;
14
+
15
+ #[ async_trait]
16
+ impl < S > FromRequestParts < S > for ExtractLoggedInUser
17
+ where
18
+ Arc < AppData > : FromRef < S > ,
19
+ S : Send + Sync ,
20
+ {
21
+ type Rejection = Response ;
22
+
23
+ async fn from_request_parts ( parts : & mut Parts , state : & S ) -> Result < Self , Self :: Rejection > {
24
+ let maybe_bearer_token = match bearer_token:: Extract :: from_request_parts ( parts, state) . await {
25
+ Ok ( maybe_bearer_token) => maybe_bearer_token. 0 ,
26
+ Err ( _) => return Err ( ServiceError :: Unauthorized . into_response ( ) ) ,
27
+ } ;
28
+
29
+ //Extracts the app state
30
+ let app_data = Arc :: from_ref ( state) ;
31
+
32
+ match app_data. auth . get_user_id_from_bearer_token ( & maybe_bearer_token) . await {
33
+ Ok ( user_id) => Ok ( ExtractLoggedInUser ( user_id) ) ,
34
+ Err ( error) => Err ( error. into_response ( ) ) ,
35
+ }
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments