guardoActionWithResult<T> method

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

Protects an action with biometric authentication and returns a result

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

Example usage:

final result = await context.guardoActionWithResult<String>(
  onSuccess: () => 'Action completed successfully',
  onFailure: (exception) => 'Authentication failed: $exception',
  reason: 'Please authenticate to perform this action',
);

Implementation

Future<T?> guardoActionWithResult<T>({
  required T Function() onSuccess,
  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 onSuccess();
    } else {
      return onFailure?.call(
        const AuthenticationFailedException(
          'Authentication was cancelled or failed',
        ),
      );
    }
  } catch (e) {
    return onFailure?.call(e);
  }
}