File tree 1 file changed +13
-0
lines changed
1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -13,18 +13,30 @@ pub struct Middleware;
13
13
14
14
impl conduit_middleware:: Middleware for Middleware {
15
15
fn before ( & self , req : & mut Request ) -> Result < ( ) , Box < Error +Send > > {
16
+ // Check if the request has a session cookie with a `user_id` property inside
16
17
let id = { req. session ( ) . get ( "user_id" ) . and_then ( |s| s. parse ( ) . ok ( ) ) } ;
18
+
17
19
let user = match id {
20
+
21
+ // `user_id` was found on the session
18
22
Some ( id) => {
23
+
24
+ // Look for a user in the database with the given `user_id`
19
25
match User :: find ( try!( req. tx ( ) . map_err ( std_error) ) , id) {
20
26
Ok ( user) => user,
21
27
Err ( ..) => return Ok ( ( ) ) ,
22
28
}
23
29
}
30
+
31
+ // `user_id` was *not* found on the session
24
32
None => {
33
+
34
+ // Look for an `Authorization` header on the request
25
35
let tx = try!( req. tx ( ) . map_err ( std_error) ) ;
26
36
match req. headers ( ) . find ( "Authorization" ) {
27
37
Some ( headers) => {
38
+
39
+ // Look for a user in the database with a matching API token
28
40
match User :: find_by_api_token ( tx, & headers[ 0 ] ) {
29
41
Ok ( user) => user,
30
42
Err ( ..) => return Ok ( ( ) )
@@ -35,6 +47,7 @@ impl conduit_middleware::Middleware for Middleware {
35
47
}
36
48
} ;
37
49
50
+ // Attach the `User` model from the database to the request
38
51
req. mut_extensions ( ) . insert ( user) ;
39
52
Ok ( ( ) )
40
53
}
You can’t perform that action at this time.
0 commit comments