@@ -2,14 +2,28 @@ import 'dart:io';
2
2
3
3
import 'package:core/core.dart' ; // For exceptions
4
4
import 'package:dart_frog/dart_frog.dart' ;
5
- import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/rate_limiter_middleware.dart' ;
6
5
import 'package:flutter_news_app_api_server_full_source_code/src/services/auth_service.dart' ;
7
6
import 'package:logging/logging.dart' ;
8
7
9
8
// Create a logger for this file.
10
9
final _logger = Logger ('request_code_handler' );
11
10
12
- Future <Response > _onRequest (RequestContext context) async {
11
+ /// Handles POST requests to `/api/v1/auth/request-code` .
12
+ ///
13
+ /// Initiates an email-based sign-in process. This endpoint is context-aware.
14
+ ///
15
+ /// - For the user-facing app, it sends a verification code to the provided
16
+ /// email, supporting both sign-in and sign-up.
17
+ /// - For the dashboard, the request body must include `"isDashboardLogin": true` .
18
+ /// In this mode, it first verifies the user exists and has 'admin' or
19
+ /// 'publisher' roles before sending a code, effectively acting as a
20
+ /// login-only gate.
21
+ Future <Response > onRequest (RequestContext context) async {
22
+ // Ensure this is a POST request
23
+ if (context.request.method != HttpMethod .post) {
24
+ return Response (statusCode: HttpStatus .methodNotAllowed);
25
+ }
26
+
13
27
// Read the AuthService provided by middleware
14
28
final authService = context.read <AuthService >();
15
29
@@ -79,30 +93,4 @@ Future<Response> _onRequest(RequestContext context) async {
79
93
}
80
94
}
81
95
82
- /// Handles POST requests to `/api/v1/auth/request-code` .
83
- ///
84
- /// Initiates an email-based sign-in process. This endpoint is context-aware.
85
- ///
86
- /// - For the user-facing app, it sends a verification code to the provided
87
- /// email, supporting both sign-in and sign-up.
88
- /// - For the dashboard, the request body must include `"isDashboardLogin": true` .
89
- /// In this mode, it first verifies the user exists and has 'admin' or
90
- /// 'publisher' roles before sending a code, effectively acting as a
91
- /// login-only gate.
92
- Future <Response > onRequest (RequestContext context) async {
93
- // Ensure this is a POST request
94
- if (context.request.method != HttpMethod .post) {
95
- return Response (statusCode: HttpStatus .methodNotAllowed);
96
- }
97
96
98
- // Apply the rate limiter middleware before calling the actual handler.
99
- final handler = const Pipeline ().addMiddleware (
100
- rateLimiter (
101
- limit: 3 ,
102
- window: const Duration (hours: 24 ),
103
- keyExtractor: ipKeyExtractor,
104
- ),
105
- ).addHandler (_onRequest);
106
-
107
- return handler (context);
108
- }
0 commit comments