Skip to content

Commit 85e4d45

Browse files
committed
feat: add authentication to /api/v1/
- Added middleware to routes - Uses authenticationProvider - Expects AuthTokenService provided
1 parent 6de806f commit 85e4d45

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

routes/_middleware.dart

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,12 @@ Handler middleware(Handler handler) {
266266
),
267267
) // Used by AuthService
268268

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) ---
269+
// --- 4. Authentication Service Providers (Auth Logic Dependencies) ---
282270
// PURPOSE: Provide the core services needed for authentication logic.
283-
// ORDER: These MUST be provided BEFORE any route handlers that perform
284-
// authentication/authorization.
271+
// ORDER: These MUST be provided BEFORE `authenticationProvider` and
272+
// any route handlers that perform authentication/authorization.
285273
// - `Uuid` is used by `AuthService` and `JwtAuthTokenService`.
286-
// - `AuthTokenService` is used by `AuthService` and read by
287-
// `authenticationProvider` (previous step).
274+
// - `AuthTokenService` is read by `authenticationProvider`.
288275
// - `AuthService` uses several repositories and `AuthTokenService`.
289276
// - `VerificationCodeStorageService` is used by `AuthService`.
290277
// - `TokenBlacklistService` is used by `JwtAuthTokenService`.
@@ -310,7 +297,7 @@ Handler middleware(Handler handler) {
310297
),
311298
) // Reads other services/repos
312299

313-
// --- 6. Request Logger (Logging) ---
300+
// --- 5. Request Logger (Logging) ---
314301
// PURPOSE: Logs details about the incoming request and outgoing response.
315302
// ORDER: Often placed late in the request phase / early in the response
316303
// phase. Placing it here logs the request *before* the handler

routes/api/v1/_middleware.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:dart_frog/dart_frog.dart';
2+
import 'package:ht_api/src/middlewares/authentication_middleware.dart';
3+
4+
Handler middleware(Handler handler) {
5+
// This middleware applies authentication to all routes under /api/v1/.
6+
// It expects AuthTokenService to be provided by an ancestor middleware
7+
// (e.g., the global routes/_middleware.dart).
8+
return handler.use(authenticationProvider());
9+
}

0 commit comments

Comments
 (0)