confirmAndRun function
- BuildContext context,
- MagicStarterProfileController controller, {
- required Future<
bool> action(), - ConfirmDialogVariant variant = ConfirmDialogVariant.primary,
- String? title,
- String? description,
Confirms identity, then runs a gated action with the proof, inside the
confirmation dialog.
The dialog stays open while the server refuses the proof itself, so the
user can retry with a fresh one: the refusal is read off controller and
shown inline, and a step_up_required refusal narrows the step-up dialog
to the proofs the server takes (MagicStarterProfileController.stepUpAccepts).
Any other refusal (a 422 on the new password, password_already_set,
owns_shared_teams, subscription_active) is not something a new proof
fixes: the dialog closes, the controller's error is kept, and a refusal
with no field errors to show is announced with a toast.
action calls a gated controller method with the proof and answers
whether it succeeded; it keeps whatever the call returned. Resolves to
true when the action succeeded, false when the user cancelled or the
action was refused for a reason other than the proof.
Implementation
Future<bool> confirmAndRun(
BuildContext context,
MagicStarterProfileController controller, {
required Future<bool> Function(Map<String, String> proof) action,
ConfirmDialogVariant variant = ConfirmDialogVariant.primary,
String? title,
String? description,
}) async {
final accepts = ValueNotifier<List<String>?>(null);
var succeeded = false;
try {
await confirmIdentity(
context,
variant: variant,
title: title,
description: description,
accepts: accepts,
attempt: (proof) async {
succeeded = await action(proof);
if (succeeded) return null;
// 1. A refusal of the proof itself: a step-up one, or a field error
// keyed by a field this proof sent (`password`, `code`,
// `confirmation_token`). The key alone is not enough: a 422 on a
// new `password` is the form's, not the proof's.
final proofRefused =
controller.stepUpAccepts != null ||
proof.keys.any(controller.hasError);
if (!proofRefused) {
// Nothing on the pages that call this renders the controller's
// error, so a refusal without field errors would pass unseen.
final message = controller.rxStatus.message;
if (message != null && controller.validationErrors.isEmpty) {
Magic.toast(message);
}
return null;
}
// 2. Read before clearing: clearing the errors clears the refusal.
accepts.value = controller.stepUpAccepts ?? accepts.value;
final message =
controller.rxStatus.message ?? trans('common.error_occurred');
controller.clearErrors();
return message;
},
);
return succeeded;
} finally {
accepts.dispose();
}
}