Skip to content

Commit c13a2ad

Browse files
committed
feat(middleware): implement data fetch and ownership check chain
- Add `dataFetchMiddleware` to retrieve data item by ID - Integrate `ownershipCheckMiddleware` for access control - Establish middleware chain for efficient request handling
1 parent 605dceb commit c13a2ad

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import 'package:dart_frog/dart_frog.dart';
2+
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/data_fetch_middleware.dart';
23
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart';
34

45
/// Middleware specific to the item-level `/api/v1/data/[id]` route path.
56
///
6-
/// This middleware applies the [ownershipCheckMiddleware] to perform an
7-
/// ownership check on the requested item *after* the parent middleware
8-
/// (`/api/v1/data/_middleware.dart`) has already performed authentication and
9-
/// authorization checks.
7+
/// This middleware chain is responsible for fetching the requested data item
8+
/// and then performing an ownership check on it.
109
///
11-
/// This ensures that only authorized users can proceed, and then this
12-
/// middleware adds the final layer of security by verifying item ownership
13-
/// for non-admin users when required by the model's configuration.
10+
/// The execution order is as follows:
11+
/// 1. `dataFetchMiddleware`: This runs first. It fetches the item by its ID
12+
/// from the database and provides it to the context. If the item is not
13+
/// found, it throws a `NotFoundException`, aborting the request.
14+
/// 2. `ownershipCheckMiddleware`: This runs second. It reads the fetched item
15+
/// from the context and verifies that the authenticated user is the owner,
16+
/// if the model's configuration requires such a check.
17+
///
18+
/// This ensures that the final route handler only executes for valid,
19+
/// authorized requests and can safely assume the requested item exists.
1420
Handler middleware(Handler handler) {
15-
// The `ownershipCheckMiddleware` will run after the middleware from
16-
// `/api/v1/data/_middleware.dart` (authn, authz, model validation).
17-
return handler.use(ownershipCheckMiddleware());
21+
// The middleware is applied in reverse order of execution.
22+
// `ownershipCheckMiddleware` is the inner middleware, running after
23+
// `dataFetchMiddleware`.
24+
return handler
25+
.use(ownershipCheckMiddleware()) // Runs second
26+
.use(dataFetchMiddleware()); // Runs first
1827
}

0 commit comments

Comments
 (0)