useRecoveryCode method

Future<bool> useRecoveryCode(
  1. String code
)

Validates a single recovery code, and if correct, marks it as used by removing it from the stored list.

Implementation

Future<bool> useRecoveryCode(String code) async {
  final codes = await getRecoveryCodes();
  final cleanCode = code.trim().replaceAll('-', '').toLowerCase();

  int index = -1;
  for (int i = 0; i < codes.length; i++) {
    if (codes[i].trim().replaceAll('-', '').toLowerCase() == cleanCode) {
      index = i;
      break;
    }
  }

  if (index != -1) {
    codes.removeAt(index);
    await saveRecoveryCodes(codes);
    return true;
  }
  return false;
}