guardoAsyncActionWithResult<T> method

Future<T?> guardoAsyncActionWithResult<T>({
  1. required Future<T> onSuccess(),
  2. Future<T> onFailure(
    1. Object exception
    )?,
  3. String? reason,
  4. GuardoConfig? config,
  5. GuardoService? guardoService,
})

Protects an async action with biometric authentication and returns a result

This method will prompt the user for biometric authentication and execute the appropriate async callback based on the result, returning the result.

Example usage:

final result = await context.guardoAsyncActionWithResult<String>(
  onSuccess: () async => await _fetchSecureData(),
  onFailure: (exception) async => 'Failed to authenticate: $exception',
  reason: 'Please authenticate to access secure data',
);

Implementation

Future<T?> guardoAsyncActionWithResult<T>({
  required Future<T> Function() onSuccess,
  Future<T> Function(Object exception)? onFailure,
  String? reason,
  GuardoConfig? config,
  GuardoService? guardoService,
}) async {
  try {
    final authenticated = await _performAuthentication(
      guardoService: guardoService,
      config: config,
      reason: reason,
    );

    if (authenticated) {
      return await onSuccess();
    } else {
      return await onFailure?.call(
        const AuthenticationFailedException(
          'Authentication was cancelled or failed',
        ),
      );
    }
  } catch (e) {
    return await onFailure?.call(e);
  }
}