@@ -266,25 +266,39 @@ Handler middleware(Handler handler) {
266
266
),
267
267
) // Used by AuthService
268
268
269
- // --- 4. Authentication Service Providers (Auth Logic Dependencies) ---
269
+ // --- 4. Authentication Middleware (User Context Population) ---
270
+ // PURPOSE: Reads the `Authorization: Bearer <token>` header, validates
271
+ // the token using `AuthTokenService`, and provides the
272
+ // resulting `User?` object into the context.
273
+ // ORDER: Empirically found to work best in this position.
274
+ // While it reads `AuthTokenService` (provided in the next step),
275
+ // this order is critical for correct runtime behavior. The
276
+ // `AuthTokenService` instance is created before the chain and
277
+ // captured by its provider closure. Should come BEFORE any
278
+ // route handlers that need `context.read<User?>()`.
279
+ .use (authenticationProvider ())
280
+
281
+ // --- 5. Authentication Service Providers (Auth Logic Dependencies) ---
270
282
// PURPOSE: Provide the core services needed for authentication logic.
271
- // ORDER: These MUST be provided BEFORE `authenticationProvider` and
272
- // any route handlers that perform authentication/authorization.
273
- // - `AuthTokenService` is read by `authenticationProvider`.
283
+ // ORDER: These MUST be provided BEFORE any route handlers that perform
284
+ // authentication/authorization.
285
+ // - `Uuid` is used by `AuthService` and `JwtAuthTokenService`.
286
+ // - `AuthTokenService` is used by `AuthService` and read by
287
+ // `authenticationProvider` (previous step).
274
288
// - `AuthService` uses several repositories and `AuthTokenService`.
275
289
// - `VerificationCodeStorageService` is used by `AuthService`.
276
290
// - `TokenBlacklistService` is used by `JwtAuthTokenService`.
277
- // - `Uuid` is used by ` AuthService` and `JwtAuthTokenService`.
291
+ . use ( provider < Uuid >((_) => uuid)) // Read by AuthService & TokenService
278
292
.use (
279
293
provider <TokenBlacklistService >(
280
294
(_) => tokenBlacklistService,
281
295
),
282
- ) // Read by JwtAuthTokenService
296
+ ) // Read by AuthTokenService
283
297
.use (
284
298
provider <AuthTokenService >(
285
299
(_) => authTokenService,
286
300
),
287
- ) // Read by authenticationProvider
301
+ ) // Read by AuthService
288
302
.use (
289
303
provider <VerificationCodeStorageService >(
290
304
(_) => verificationCodeStorageService,
@@ -295,16 +309,6 @@ Handler middleware(Handler handler) {
295
309
(_) => authService,
296
310
),
297
311
) // Reads other services/repos
298
- .use (provider <Uuid >((_) => uuid)) // Read by AuthService & TokenService
299
-
300
- // --- 5. Authentication Middleware (User Context Population) ---
301
- // PURPOSE: Reads the `Authorization: Bearer <token>` header, validates
302
- // the token using `AuthTokenService`, and provides the
303
- // resulting `User?` object into the context.
304
- // ORDER: MUST come AFTER `AuthTokenService` is provided (which it reads).
305
- // Should come BEFORE any route handlers that need to know the
306
- // currently authenticated user (`context.read<User?>()`).
307
- .use (authenticationProvider ())
308
312
309
313
// --- 6. Request Logger (Logging) ---
310
314
// PURPOSE: Logs details about the incoming request and outgoing response.
0 commit comments