From 837972ab778158a9c68f876bf84e86ee838ed5fa Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:11:57 +0100 Subject: [PATCH 1/9] refactor(auth_service): replace Uuid with mongo_dart ObjectId - Remove uuid package and replace with mongo_dart package - Update user ID generation to use ObjectId.oid instead of uuid.v4() - Modify anonymous user email to use ObjectId instead of uuid - Remove Uuid dependency from AuthService constructor --- lib/src/services/auth_service.dart | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/src/services/auth_service.dart b/lib/src/services/auth_service.dart index df98aad..acd4e9d 100644 --- a/lib/src/services/auth_service.dart +++ b/lib/src/services/auth_service.dart @@ -6,7 +6,7 @@ import 'package:ht_data_repository/ht_data_repository.dart'; import 'package:ht_email_repository/ht_email_repository.dart'; import 'package:ht_shared/ht_shared.dart'; import 'package:logging/logging.dart'; -import 'package:uuid/uuid.dart'; +import 'package:mongo_dart/mongo_dart.dart'; /// {@template auth_service} /// Service responsible for orchestrating authentication logic on the backend. @@ -25,7 +25,6 @@ class AuthService { required HtDataRepository userContentPreferencesRepository, required PermissionService permissionService, - required Uuid uuidGenerator, required Logger log, }) : _userRepository = userRepository, _authTokenService = authTokenService, @@ -34,7 +33,6 @@ class AuthService { _emailRepository = emailRepository, _userAppSettingsRepository = userAppSettingsRepository, _userContentPreferencesRepository = userContentPreferencesRepository, - _uuid = uuidGenerator, _log = log; final HtDataRepository _userRepository; @@ -46,7 +44,6 @@ class AuthService { _userContentPreferencesRepository; final PermissionService _permissionService; final Logger _log; - final Uuid _uuid; /// Initiates the email sign-in process. /// @@ -212,7 +209,7 @@ class AuthService { // All new users created via the public API get the standard role. // Admin users must be provisioned out-of-band (e.g., via fixtures). user = User( - id: _uuid.v4(), + id: ObjectId().oid, email: email, appRole: AppUserRole.standardUser, dashboardRole: DashboardUserRole.none, @@ -268,11 +265,12 @@ class AuthService { // 1. Create anonymous user User user; try { + final newId = ObjectId().oid; user = User( - id: _uuid.v4(), + id: newId, // Use a unique placeholder email for anonymous users to satisfy the // non-nullable email constraint. - email: '${_uuid.v4()}@anonymous.com', + email: '$newId@anonymous.com', appRole: AppUserRole.guestUser, dashboardRole: DashboardUserRole.none, createdAt: DateTime.now(), From 63ac69976cb127ccfdc57b25f23a66c788ab14ef Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:12:13 +0100 Subject: [PATCH 2/9] fix(auth): remove uuidGenerator from AuthService dependencies - Removed the uuidGenerator parameter from the AuthService instantiation - This change simplifies the AuthService dependencies and improves code maintainability --- lib/src/config/app_dependencies.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/config/app_dependencies.dart b/lib/src/config/app_dependencies.dart index 2fd2018..c2fb0d6 100644 --- a/lib/src/config/app_dependencies.dart +++ b/lib/src/config/app_dependencies.dart @@ -186,7 +186,6 @@ class AppDependencies { emailRepository: emailRepository, userAppSettingsRepository: userAppSettingsRepository, userContentPreferencesRepository: userContentPreferencesRepository, - uuidGenerator: const Uuid(), log: Logger('AuthService'), ); dashboardSummaryService = DashboardSummaryService( From 4347fd0f9e7962004df5327e40643fc287c78fa7 Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:13:16 +0100 Subject: [PATCH 3/9] feat(api): automatically add ID and timestamps to created documents - Implement automatic generation of 'id', 'createdAt', and 'updatedAt' fields for new documents - Use ObjectId for 'id' and ISO 8601 string for timestamps - Ensure UTC time is used for timestamps --- routes/api/v1/data/index.dart | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/routes/api/v1/data/index.dart b/routes/api/v1/data/index.dart index eef39d3..6a5fd42 100644 --- a/routes/api/v1/data/index.dart +++ b/routes/api/v1/data/index.dart @@ -7,6 +7,7 @@ import 'package:ht_api/src/rbac/permission_service.dart'; import 'package:ht_api/src/registry/model_registry.dart'; import 'package:ht_data_repository/ht_data_repository.dart'; import 'package:ht_shared/ht_shared.dart'; +import 'package:mongo_dart/mongo_dart.dart'; /// Handles requests for the /api/v1/data collection endpoint. /// Dispatches requests to specific handlers based on the HTTP method. @@ -152,6 +153,12 @@ Future _handlePost(RequestContext context) async { throw const BadRequestException('Missing or invalid request body.'); } + // Standardize ID and timestamps before model creation + final now = DateTime.now().toUtc().toIso8601String(); + requestBody['id'] = ObjectId().oid; + requestBody['createdAt'] = now; + requestBody['updatedAt'] = now; + dynamic itemToCreate; try { itemToCreate = modelConfig.fromJson(requestBody); From d7e24c83505ff5a8a1ac7a5abcc92f0331fef1b5 Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:15:16 +0100 Subject: [PATCH 4/9] feat(api): add timestamp update for PUT requests - Add server-side timestamp update for data modification - Ensure 'updatedAt' field is always synchronized with the last modification time - Improve data consistency and tracking --- routes/api/v1/data/[id]/index.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/routes/api/v1/data/[id]/index.dart b/routes/api/v1/data/[id]/index.dart index 11b90dc..b390ebe 100644 --- a/routes/api/v1/data/[id]/index.dart +++ b/routes/api/v1/data/[id]/index.dart @@ -185,6 +185,9 @@ Future _handlePut( throw const BadRequestException('Missing or invalid request body.'); } + // Standardize timestamp before model creation + requestBody['updatedAt'] = DateTime.now().toUtc().toIso8601String(); + // Deserialize using ModelConfig's fromJson, catching TypeErrors locally dynamic itemToUpdate; try { From 4e4fc0418524136117a090d845be4251f3ac317f Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:19:07 +0100 Subject: [PATCH 5/9] refactor(auth): replace UUID with MongoDB ObjectId for JWT ID - Remove Uuid dependency and replace it with mongo_dart ObjectId - Update JWT claims to use ObjectId.oid for 'jti' (JWT ID) - Adjust constructor to remove Uuid parameter - Update import statements to reflect changes --- lib/src/services/jwt_auth_token_service.dart | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/src/services/jwt_auth_token_service.dart b/lib/src/services/jwt_auth_token_service.dart index 5f7774f..41ab1bf 100644 --- a/lib/src/services/jwt_auth_token_service.dart +++ b/lib/src/services/jwt_auth_token_service.dart @@ -1,10 +1,12 @@ import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; import 'package:ht_api/src/services/auth_token_service.dart'; +import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; +import 'package:ht_api/src/services/auth_token_service.dart'; import 'package:ht_api/src/services/token_blacklist_service.dart'; import 'package:ht_data_repository/ht_data_repository.dart'; import 'package:ht_shared/ht_shared.dart'; import 'package:logging/logging.dart'; -import 'package:uuid/uuid.dart'; +import 'package:mongo_dart/mongo_dart.dart'; /// {@template jwt_auth_token_service} /// An implementation of [AuthTokenService] using JSON Web Tokens (JWT). @@ -19,20 +21,16 @@ class JwtAuthTokenService implements AuthTokenService { /// - [userRepository]: To fetch user details after validating the token's /// subject claim. /// - [blacklistService]: To manage the blacklist of invalidated tokens. - /// - [uuidGenerator]: For creating unique JWT IDs (jti). const JwtAuthTokenService({ required HtDataRepository userRepository, required TokenBlacklistService blacklistService, - required Uuid uuidGenerator, required Logger log, }) : _userRepository = userRepository, _blacklistService = blacklistService, - _uuid = uuidGenerator, _log = log; final HtDataRepository _userRepository; final TokenBlacklistService _blacklistService; - final Uuid _uuid; final Logger _log; // --- Configuration --- @@ -61,7 +59,7 @@ class JwtAuthTokenService implements AuthTokenService { 'exp': expiry.millisecondsSinceEpoch ~/ 1000, // Expiration Time 'iat': now.millisecondsSinceEpoch ~/ 1000, // Issued At 'iss': _issuer, // Issuer - 'jti': _uuid.v4(), // JWT ID (for potential blacklisting) + 'jti': ObjectId().oid, // JWT ID (for potential blacklisting) // Custom claims (optional, include what's useful) 'email': user.email, // Kept for convenience // Embed the new enum-based roles. Use .name for string value. @@ -70,7 +68,7 @@ class JwtAuthTokenService implements AuthTokenService { }, issuer: _issuer, subject: user.id, - jwtId: _uuid.v4(), // Re-setting jti here for clarity if needed + jwtId: ObjectId().oid, // Re-setting jti here for clarity if needed ); // Sign the token using HMAC-SHA256 From 06b1836824f856428855bddd1550e15d3aecee3d Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:19:18 +0100 Subject: [PATCH 6/9] fix(auth): remove explicit Uuid dependency from JwtAuthTokenService - Remove unnecessary Uuid dependency from JwtAuthTokenService - JwtAuthTokenService should provide its own UuidGenerator if needed --- lib/src/config/app_dependencies.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/config/app_dependencies.dart b/lib/src/config/app_dependencies.dart index c2fb0d6..2f5f6a5 100644 --- a/lib/src/config/app_dependencies.dart +++ b/lib/src/config/app_dependencies.dart @@ -173,7 +173,6 @@ class AppDependencies { authTokenService = JwtAuthTokenService( userRepository: userRepository, blacklistService: tokenBlacklistService, - uuidGenerator: const Uuid(), log: Logger('JwtAuthTokenService'), ); verificationCodeStorageService = InMemoryVerificationCodeStorageService(); From ca3cedfe0b43fefefc8e5f07c9ca38763623cd68 Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:25:34 +0100 Subject: [PATCH 7/9] refactor(dependencies): replace Uuid with ObjectId for request ID generation - Remove uuid package and import mongo_dart package instead - Replace Uuid generation with ObjectId in request ID creation - Remove Uuid provider from middleware --- routes/_middleware.dart | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/routes/_middleware.dart b/routes/_middleware.dart index d93bd9d..3984ed0 100644 --- a/routes/_middleware.dart +++ b/routes/_middleware.dart @@ -14,7 +14,7 @@ import 'package:ht_data_repository/ht_data_repository.dart'; import 'package:ht_email_repository/ht_email_repository.dart'; import 'package:ht_shared/ht_shared.dart'; import 'package:logging/logging.dart'; -import 'package:uuid/uuid.dart'; +import 'package:mongo_dart/mongo_dart.dart'; // --- Middleware Definition --- final _log = Logger('RootMiddleware'); @@ -55,8 +55,7 @@ Handler middleware(Handler handler) { _log.info( '[REQ_LIFECYCLE] Request received. Generating RequestId...', ); - final uuid = context.read(); - final requestId = RequestId(uuid.v4()); + final requestId = RequestId(ObjectId().oid); _log.info('[REQ_LIFECYCLE] RequestId generated: ${requestId.id}'); return innerHandler(context.provide(() => requestId)); }; @@ -76,7 +75,6 @@ Handler middleware(Handler handler) { final deps = AppDependencies.instance; return handler .use(provider((_) => modelRegistry)) - .use(provider((_) => const Uuid())) .use( provider >( (_) => deps.headlineRepository, From b65e9c5f7c2f824a644e8f3a17a51b90ee6a16b2 Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:25:46 +0100 Subject: [PATCH 8/9] build(deps): remove unused uuid dependency - Removed uuid package from dependencies in pubspec.yaml - This change simplifies the project dependencies and reduces potential bloat --- pubspec.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 96e31e9..b4fe4dd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,7 +39,6 @@ dependencies: meta: ^1.16.0 mongo_dart: ^0.10.5 shelf_cors_headers: ^0.1.5 - uuid: ^4.5.1 dev_dependencies: mocktail: ^1.0.3 From 8c29087b19e21fefdc105af3f8f9199e99c1c27d Mon Sep 17 00:00:00 2001 From: fulleni Date: 2025年7月20日 07:26:09 +0100 Subject: [PATCH 9/9] lint: fix --- lib/src/config/app_dependencies.dart | 1 - lib/src/services/jwt_auth_token_service.dart | 2 -- 2 files changed, 3 deletions(-) diff --git a/lib/src/config/app_dependencies.dart b/lib/src/config/app_dependencies.dart index 2f5f6a5..7f79b38 100644 --- a/lib/src/config/app_dependencies.dart +++ b/lib/src/config/app_dependencies.dart @@ -15,7 +15,6 @@ import 'package:ht_email_inmemory/ht_email_inmemory.dart'; import 'package:ht_email_repository/ht_email_repository.dart'; import 'package:ht_shared/ht_shared.dart'; import 'package:logging/logging.dart'; -import 'package:uuid/uuid.dart'; /// {@template app_dependencies} /// A singleton class responsible for initializing and providing all application diff --git a/lib/src/services/jwt_auth_token_service.dart b/lib/src/services/jwt_auth_token_service.dart index 41ab1bf..4aaa86a 100644 --- a/lib/src/services/jwt_auth_token_service.dart +++ b/lib/src/services/jwt_auth_token_service.dart @@ -1,7 +1,5 @@ import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; import 'package:ht_api/src/services/auth_token_service.dart'; -import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; -import 'package:ht_api/src/services/auth_token_service.dart'; import 'package:ht_api/src/services/token_blacklist_service.dart'; import 'package:ht_data_repository/ht_data_repository.dart'; import 'package:ht_shared/ht_shared.dart';

AltStyle によって変換されたページ (->オリジナル) /