Skip to content

Commit

Permalink
Add optional custom biometricsAuthDataSource for plugin methods
Browse files Browse the repository at this point in the history
  • Loading branch information
GoranPrime committed Oct 31, 2024
1 parent 31c5a5d commit 6b78cf6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class PinCodeBloc extends $PinCodeBloc {

/// Authenticates the user with biometrics after which the pin code is
/// retrieved from the device and checked.
Future<bool> _authenticateWithBiometrics() async {
Future<bool?> _authenticateWithBiometrics() async {
if (!await biometricAuthenticationService.isDeviceSupported) {
throw ErrorEnableBiometrics(BiometricsMessage.notSupported);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:widget_toolkit_biometrics/widget_toolkit_biometrics.dart';

import '../../../widget_toolkit_pin.dart';
import '../data_source/biometrics_disabled_local_data_source.dart';
import '../data_source/pin_biometrics_auth_data_source.dart';
import '../repositories/pin_biometrics_repository.dart';
import '../services/pin_biometrics_service.dart';

Expand All @@ -14,22 +13,26 @@ class PinCodeDependencies {
this.pinCodeService,
this.biometricsLocalDataSource,
this.localizedReason,
this.biometricsAuthDataSource,
);

factory PinCodeDependencies.from({
required PinCodeService pinCodeService,
required BiometricsLocalDataSource? biometricsLocalDataSource,
required String localizedReason,
PinBiometricsAuthDataSource? biometricsAuthDataSource,
}) =>
PinCodeDependencies._(
pinCodeService,
biometricsLocalDataSource,
localizedReason,
biometricsAuthDataSource,
);

final String localizedReason;
final PinCodeService pinCodeService;
final BiometricsLocalDataSource? biometricsLocalDataSource;
final PinBiometricsAuthDataSource? biometricsAuthDataSource;

late List<SingleChildWidget> providers = [
..._localAuthentication,
Expand All @@ -47,9 +50,11 @@ class PinCodeDependencies {

late final List<SingleChildWidget> _dataSources = [
Provider<PinBiometricsAuthDataSource>(
create: (context) => PinBiometricsAuthDataSource(
localAuthentication: context.read<LocalAuthentication>(),
),
create: (context) =>
biometricsAuthDataSource ??
PinBiometricsAuthDataSource(
localAuthentication: context.read<LocalAuthentication>(),
),
),
if (biometricsLocalDataSource == null)
Provider<BiometricsLocalDataSource>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class PinCodeKeyboard extends StatelessWidget {
required this.pinCodeService,
required this.translateError,
this.biometricsLocalDataSource,
this.biometricsAuthDataSource,
this.mapBiometricMessageToString,
this.onAuthenticated,
this.deleteKeyButton,
Expand All @@ -72,6 +73,11 @@ class PinCodeKeyboard extends StatelessWidget {
/// for the package
final BiometricsLocalDataSource? biometricsLocalDataSource;

/// Provides a contract for custom biometrics authentication plugin. If this
/// parameter is not provided, a default one will be used featuring
/// [LocalAuthentication] from the local_auth package
final PinBiometricsAuthDataSource? biometricsAuthDataSource;

/// Callback called when the user authentication succeeds. It accepts a dynamic
/// value which is forwarded from the `verifyPinCode` method of the [pinCodeService].
final Function(dynamic)? onAuthenticated;
Expand Down Expand Up @@ -129,6 +135,7 @@ class PinCodeKeyboard extends StatelessWidget {
...PinCodeDependencies.from(
pinCodeService: pinCodeService,
biometricsLocalDataSource: biometricsLocalDataSource,
biometricsAuthDataSource: biometricsAuthDataSource,
localizedReason: localizedReason ?? _enterPinWithBiometrics,
).providers,
],
Expand Down
1 change: 1 addition & 0 deletions packages/widget_toolkit_pin/lib/widget_toolkit_pin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library widget_toolkit_pin;

export 'src/base/utils/theme/pin_code_theme.dart';
export 'src/lib_pin_code_with_biometrics/blocs/pin_code_bloc.dart';
export 'src/lib_pin_code_with_biometrics/data_source/pin_biometrics_auth_data_source.dart';
export 'src/lib_pin_code_with_biometrics/services/pin_code_service.dart';
export 'src/lib_pin_code_with_biometrics/ui_components/pin_code_custom_key.dart';
export 'src/lib_pin_code_with_biometrics/ui_components/pin_code_keyboard.dart';

0 comments on commit 6b78cf6

Please sign in to comment.