guardoAsyncAction method

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

Protects an async action with biometric authentication

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

Example usage:

await context.guardoAsyncAction(
  onSuccess: () async {
    // Perform protected async action
    await _uploadSecureData();
  },
  onFailure: (exception) async {
    // Handle authentication failure
    await _logAuthenticationFailure(exception);
  },
  reason: 'Please authenticate to upload data',
);

Implementation

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

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