@@ -12,10 +12,10 @@ use crate::schema::users;
12
12
#[ derive( Debug , Clone , Copy ) ]
13
13
pub struct CurrentUser ;
14
14
15
- #[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
15
+ #[ derive( Debug , Clone , Eq , PartialEq ) ]
16
16
pub enum AuthenticationSource {
17
17
SessionCookie ,
18
- ApiToken ,
18
+ ApiToken { auth_header : String } ,
19
19
}
20
20
21
21
impl Middleware for CurrentUser {
@@ -42,18 +42,19 @@ impl Middleware for CurrentUser {
42
42
} else {
43
43
// Otherwise, look for an `Authorization` header on the request
44
44
// and try to find a user in the database with a matching API token
45
- let user = if let Some ( headers ) = req. headers ( ) . find ( "Authorization" ) {
46
- User :: find_by_api_token ( & conn , headers[ 0 ] )
47
- . optional ( )
48
- . map_err ( |e| Box :: new ( e ) as Box < dyn Error + Send > ) ?
49
- } else {
50
- None
51
- } ;
45
+ let user_auth = req. headers ( ) . find ( "Authorization" ) . and_then ( |headers| {
46
+ let auth_header = headers[ 0 ] . to_string ( ) ;
47
+
48
+ User :: find_by_api_token ( & conn , & auth_header )
49
+ . ok ( )
50
+ . map ( |user| ( AuthenticationSource :: ApiToken { auth_header } , user ) )
51
+ } ) ;
52
52
drop ( conn) ;
53
- if let Some ( user) = user {
53
+
54
+ if let Some ( ( api_token, user) ) = user_auth {
54
55
// Attach the `User` model from the database to the request
55
56
req. mut_extensions ( ) . insert ( user) ;
56
- req. mut_extensions ( ) . insert ( AuthenticationSource :: ApiToken ) ;
57
+ req. mut_extensions ( ) . insert ( api_token ) ;
57
58
}
58
59
}
59
60
0 commit comments