feat(data-connect): Add run method for centralized error handling and authentication checks
This commit is contained in:
@@ -3,6 +3,7 @@ import 'dart:async';
|
|||||||
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
|
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
|
||||||
import 'package:firebase_data_connect/firebase_data_connect.dart' as fdc;
|
import 'package:firebase_data_connect/firebase_data_connect.dart' as fdc;
|
||||||
import 'package:krow_core/core.dart';
|
import 'package:krow_core/core.dart';
|
||||||
|
import 'package:krow_domain/krow_domain.dart';
|
||||||
|
|
||||||
import '../../krow_data_connect.dart' as dc;
|
import '../../krow_data_connect.dart' as dc;
|
||||||
import '../mixins/data_error_handler.dart';
|
import '../mixins/data_error_handler.dart';
|
||||||
@@ -20,6 +21,7 @@ class DataConnectService with DataErrorHandler {
|
|||||||
final dc.ExampleConnector connector = dc.ExampleConnector.instance;
|
final dc.ExampleConnector connector = dc.ExampleConnector.instance;
|
||||||
|
|
||||||
/// The Firebase Auth instance.
|
/// The Firebase Auth instance.
|
||||||
|
firebase_auth.FirebaseAuth get auth => _auth;
|
||||||
final firebase_auth.FirebaseAuth _auth = firebase_auth.FirebaseAuth.instance;
|
final firebase_auth.FirebaseAuth _auth = firebase_auth.FirebaseAuth.instance;
|
||||||
|
|
||||||
/// Cache for the current staff ID to avoid redundant lookups.
|
/// Cache for the current staff ID to avoid redundant lookups.
|
||||||
@@ -97,6 +99,20 @@ class DataConnectService with DataErrorHandler {
|
|||||||
return fdc.Timestamp(nanoseconds, seconds);
|
return fdc.Timestamp(nanoseconds, seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- 3. Unified Execution ---
|
||||||
|
// Repositories call this to benefit from centralized error handling/logging
|
||||||
|
Future<T> run<T>(
|
||||||
|
Future<T> 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).
|
/// Clears the internal cache (e.g., on logout).
|
||||||
void clearCache() {
|
void clearCache() {
|
||||||
_cachedStaffId = null;
|
_cachedStaffId = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user