authorized static method
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();
}