From 3245c957f6cb465973696bbae4e66ebce5ea3d9d Mon Sep 17 00:00:00 2001 From: Achintha Isuru Date: Mon, 16 Feb 2026 15:39:40 -0500 Subject: [PATCH] feat(data-connect): Add run method for centralized error handling and authentication checks --- .../lib/src/services/data_connect_service.dart | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apps/mobile/packages/data_connect/lib/src/services/data_connect_service.dart b/apps/mobile/packages/data_connect/lib/src/services/data_connect_service.dart index cdfe2813..c91c34d1 100644 --- a/apps/mobile/packages/data_connect/lib/src/services/data_connect_service.dart +++ b/apps/mobile/packages/data_connect/lib/src/services/data_connect_service.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:firebase_auth/firebase_auth.dart' as firebase_auth; import 'package:firebase_data_connect/firebase_data_connect.dart' as fdc; import 'package:krow_core/core.dart'; +import 'package:krow_domain/krow_domain.dart'; import '../../krow_data_connect.dart' as dc; import '../mixins/data_error_handler.dart'; @@ -20,6 +21,7 @@ class DataConnectService with DataErrorHandler { final dc.ExampleConnector connector = dc.ExampleConnector.instance; /// The Firebase Auth instance. + firebase_auth.FirebaseAuth get auth => _auth; final firebase_auth.FirebaseAuth _auth = firebase_auth.FirebaseAuth.instance; /// Cache for the current staff ID to avoid redundant lookups. @@ -97,6 +99,20 @@ class DataConnectService with DataErrorHandler { return fdc.Timestamp(nanoseconds, seconds); } + // --- 3. Unified Execution --- + // Repositories call this to benefit from centralized error handling/logging + Future run( + Future Function() action, { + bool requiresAuthentication = true, + }) { + if (requiresAuthentication && auth.currentUser == null) { + throw const NotAuthenticatedException( + technicalMessage: 'User must be authenticated to perform this action', + ); + } + return executeProtected(action); + } + /// Clears the internal cache (e.g., on logout). void clearCache() { _cachedStaffId = null;