addUserWithBiometrics function

Future<Map<String, dynamic>> addUserWithBiometrics(
  1. String userId
)

Añade un userId a la lista de usuarios con biometría habilitada. Retorna un Map con { success, message, code, error }.

Implementation

Future<Map<String, dynamic>> addUserWithBiometrics(String userId) async {
  if (userId.trim().isEmpty) {
    return {
      'success': false,
      'message': 'userId no proporcionado',
      'code': 'BiometricInvalidUserId',
      'error': null,
    };
  }

  try {
    final raw = await _secureStorage.read(key: _biometricUsersKey);
    final List<String> users = raw != null && raw.isNotEmpty
        ? List<String>.from(json.decode(raw) as List)
        : <String>[];

    if (!users.contains(userId)) {
      users.add(userId);
      await _secureStorage.write(
        key: _biometricUsersKey,
        value: json.encode(users),
        aOptions: const AndroidOptions(encryptedSharedPreferences: true),
      );
      return {
        'success': true,
        'message': 'Usuario agregado a la lista de biometría',
        'code': 'BiometricUserAdded',
        'error': null,
      };
    } else {
      return {
        'success': false,
        'message': 'El usuario ya estaba en la lista de biometría',
        'code': 'BiometricUserAlreadyExists',
        'error': null,
      };
    }
  } catch (e) {
    debugPrint('Error en addUserWithBiometrics: $e');
    return {
      'success': false,
      'message': 'Error al guardar el usuario con biometría',
      'code': 'BiometricStorageError',
      'error': e.toString(),
    };
  }
}