@@ -266,25 +266,39 @@ Handler middleware(Handler handler) {
266266 ),
267267 ) // Used by AuthService
268268
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) ---
270282 // 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).
274288 // - `AuthService` uses several repositories and `AuthTokenService`.
275289 // - `VerificationCodeStorageService` is used by `AuthService`.
276290 // - `TokenBlacklistService` is used by `JwtAuthTokenService`.
277- // - `Uuid` is used by ` AuthService` and `JwtAuthTokenService`.
291+ . use ( provider < Uuid >((_) => uuid)) // Read by AuthService & TokenService
278292 .use (
279293 provider <TokenBlacklistService >(
280294 (_) => tokenBlacklistService,
281295 ),
282- ) // Read by JwtAuthTokenService
296+ ) // Read by AuthTokenService
283297 .use (
284298 provider <AuthTokenService >(
285299 (_) => authTokenService,
286300 ),
287- ) // Read by authenticationProvider
301+ ) // Read by AuthService
288302 .use (
289303 provider <VerificationCodeStorageService >(
290304 (_) => verificationCodeStorageService,
@@ -295,16 +309,6 @@ Handler middleware(Handler handler) {
295309 (_) => authService,
296310 ),
297311 ) // 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 ())
308312
309313 // --- 6. Request Logger (Logging) ---
310314 // PURPOSE: Logs details about the incoming request and outgoing response.
0 commit comments