authorized static method

Future<void> authorized(
  1. FutureOr<void> perform(), {
  2. required FutureOr<bool> when(),
  3. FutureOr<void> unauthorized()?,
})

Perform an action only if the user is authorized to do so. Provide a perform callback to execute if the user is authorized. Provide a when callback to check if the user is authorized. Provide an unauthorized callback to execute if the user is not authorized.

Implementation

static Future<void> authorized(
  FutureOr<void> Function() perform, {
  required FutureOr<bool> Function() when,
  FutureOr<void> Function()? unauthorized,
}) async {
  final canPerform = await when();
  if (!canPerform) {
    await unauthorized?.call();
    return;
  }
  await perform();
}