handleBiometricAuth function

Future<ApiResult> handleBiometricAuth()

Implementation

Future<ApiResult> handleBiometricAuth() async {
  try {
    // Comprueba si el dispositivo soporta biometría
    final bool isDeviceSupported = await _localAuth.isDeviceSupported();
    final bool canCheckBiometrics = await _localAuth.canCheckBiometrics;
    if (!isDeviceSupported && !canCheckBiometrics) {
      return ApiResult(
        success: false,
        message: 'Dispositivo no compatible con biometría',
        code: 'BiometricNotSupported',
        error: null,
      );
    }

    final bool didAuthenticate = await _localAuth.authenticate(
      localizedReason: 'Autentícate para acceder a tu certificado',
      options: const AuthenticationOptions(
        biometricOnly: false,
        stickyAuth: false,
        useErrorDialogs: true,
      ),
    );

    if (didAuthenticate) {
      return ApiResult(
        success: true,
        message: 'Autenticación exitosa',
        code: 'BiometricSuccess',
        error: null,
      );
    } else {
      return ApiResult(
        success: false,
        message: 'Autenticación fallida',
        code: 'BiometricFailed',
        error: null,
      );
    }
  } catch (e) {
    debugPrint('Error en handleBiometricAuth: $e');
    return ApiResult(
      success: false,
      message: 'Error durante la autenticación biométrica',
      code: 'BiometricAuthError',
      error: e.toString(),
    );
  }
}