secureAction method

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

Protects an action with biometric authentication

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

Example usage:

context.secureAction(
  onSuccess: () {
    // Perform protected action
    _deleteAccount();
  },
  onFailure: (exception) {
    // Handle authentication failure
    _showErrorMessage('Authentication failed: $exception');
  },
  reason: 'Please authenticate to delete your account',
);

Implementation

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