submit method

Future<bool> submit({
  1. required String currentPassword,
  2. required String newPassword,
  3. required String confirmPassword,
  4. required bool isExpired,
  5. required String? username,
})

Implementation

Future<bool> submit({
  required String currentPassword,
  required String newPassword,
  required String confirmPassword,
  required bool isExpired,
  required String? username,
}) async {
  // 1. Basic empty check
  if (!isExpired && currentPassword.isEmpty) {
    state = state.copyWith(
      errorMessage: t.changePassword.errors.emptyFields,
      currentHasError: true,
    );
    return false;
  }

  if (newPassword.isEmpty || confirmPassword.isEmpty) {
    state = state.copyWith(
      errorMessage: t.changePassword.errors.emptyFields,
      newHasError: newPassword.isEmpty,
      confirmHasError: confirmPassword.isEmpty,
    );
    return false;
  }

  // 2. Mismatch check
  if (newPassword != confirmPassword) {
    state = state.copyWith(
      errorMessage: t.changePassword.errors.mismatch,
      newHasError: true,
      confirmHasError: true,
    );
    return false;
  }

  if (isExpired && (username == null || username.trim().isEmpty)) {
    state = state.copyWith(
      errorMessage: t.changePassword.errors.failed(
        error: 'Student ID is missing',
      ),
      newHasError: true,
    );
    return false;
  }

  // Determine the student ID for similarity validation
  String? studentId = username;
  if (!isExpired) {
    var user = ref.read(userProfileProvider).value;
    if (user == null) {
      final db = ref.read(databaseProvider);
      user = await db.select(db.users).getSingleOrNull();
    }
    studentId = user?.studentId;
  }

  // 3. Complexity & Similarity Validation
  final validationError = _validatePasswordRules(newPassword, studentId);
  if (validationError != null) {
    state = state.copyWith(
      errorMessage: validationError,
      newHasError: true,
      confirmHasError: true,
    );
    return false;
  }

  state = state.copyWith(
    isLoading: true,
    errorMessage: null,
    currentHasError: false,
    newHasError: false,
    confirmHasError: false,
  );

  try {
    final authRepo = ref.read(authRepositoryProvider);
    if (isExpired) {
      await authRepo.changeExpiredPassword(username!, newPassword);
    } else {
      await authRepo.changePassword(currentPassword, newPassword);
    }
    state = state.copyWith(isLoading: false);
    return true;
  } catch (e) {
    state = state.copyWith(
      isLoading: false,
      errorMessage: t.changePassword.errors.failed(error: _errorMessageOf(e)),
      currentHasError: !isExpired,
      newHasError: true,
      confirmHasError: true,
    );
    return false;
  }
}