Skip to content

Commit 65f76a2

Browse files
committed
refactor(auth): move request-code handler to index.dart and apply rate limiting
- Rename request-code.dart to index.dart for better modularity - Implement rate limiting middleware directly in the handler - Improve code structure and prepare for additional endpoint implementations
1 parent 9453122 commit 65f76a2

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

routes/api/v1/auth/request-code.dart renamed to routes/api/v1/auth/request-code/index.dart

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@ import 'dart:io';
22

33
import 'package:core/core.dart'; // For exceptions
44
import 'package:dart_frog/dart_frog.dart';
5-
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/rate_limiter_middleware.dart';
65
import 'package:flutter_news_app_api_server_full_source_code/src/services/auth_service.dart';
76
import 'package:logging/logging.dart';
87

98
// Create a logger for this file.
109
final _logger = Logger('request_code_handler');
1110

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+
1327
// Read the AuthService provided by middleware
1428
final authService = context.read<AuthService>();
1529

@@ -79,30 +93,4 @@ Future<Response> _onRequest(RequestContext context) async {
7993
}
8094
}
8195

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-
}
9796

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

Comments
 (0)