|
1 | 1 | import 'package:dart_frog/dart_frog.dart';
|
| 2 | +import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/data_fetch_middleware.dart'; |
2 | 3 | import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/ownership_check_middleware.dart';
|
3 | 4 |
|
4 | 5 | /// Middleware specific to the item-level `/api/v1/data/[id]` route path.
|
5 | 6 | ///
|
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. |
10 | 9 | ///
|
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. |
14 | 20 | 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 |
18 | 27 | }
|
0 commit comments