Skip to content

Commit 6aa2cff

Browse files
committed
user/middleware: Add comments to Middleware implementation
1 parent f1b7264 commit 6aa2cff

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/user/middleware.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@ pub struct Middleware;
1313

1414
impl conduit_middleware::Middleware for Middleware {
1515
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
1617
let id = { req.session().get("user_id").and_then(|s| s.parse().ok()) };
18+
1719
let user = match id {
20+
21+
// `user_id` was found on the session
1822
Some(id) => {
23+
24+
// Look for a user in the database with the given `user_id`
1925
match User::find(try!(req.tx().map_err(std_error)), id) {
2026
Ok(user) => user,
2127
Err(..) => return Ok(()),
2228
}
2329
}
30+
31+
// `user_id` was *not* found on the session
2432
None => {
33+
34+
// Look for an `Authorization` header on the request
2535
let tx = try!(req.tx().map_err(std_error));
2636
match req.headers().find("Authorization") {
2737
Some(headers) => {
38+
39+
// Look for a user in the database with a matching API token
2840
match User::find_by_api_token(tx, &headers[0]) {
2941
Ok(user) => user,
3042
Err(..) => return Ok(())
@@ -35,6 +47,7 @@ impl conduit_middleware::Middleware for Middleware {
3547
}
3648
};
3749

50+
// Attach the `User` model from the database to the request
3851
req.mut_extensions().insert(user);
3952
Ok(())
4053
}

0 commit comments

Comments
 (0)