authenticate method

  1. @override
Future<bool> authenticate({
  1. required String localizedReason,
  2. required Iterable<AuthMessages> authMessages,
  3. AuthenticationOptions options = const AuthenticationOptions(),
})
override

Authenticates the user with biometrics available on the device while also allowing the user to use device authentication - pin, pattern, passcode.

Returns true if the user successfully authenticated, false otherwise.

localizedReason is the message to show to user while prompting them for authentication. This is typically along the lines of: 'Please scan your finger to access MyApp.'. This must not be empty.

Provide authMessages if you want to customize messages in the dialogs.

Provide options for configuring further authentication related options.

Throws a PlatformException if there were technical problems with local authentication (e.g. lack of relevant hardware). This might throw PlatformException with error code otherOperatingSystem on the iOS simulator.

Implementation

@override
Future<bool> authenticate({
  required String localizedReason,
  required Iterable<AuthMessages> authMessages,
  AuthenticationOptions options = const AuthenticationOptions(),
}) async {
  assert(localizedReason.isNotEmpty);
  final AuthResult result = await _api.authenticate(
      AuthOptions(
          biometricOnly: options.biometricOnly,
          sensitiveTransaction: options.sensitiveTransaction,
          sticky: options.stickyAuth,
          useErrorDialgs: options.useErrorDialogs),
      _pigeonStringsFromAuthMessages(localizedReason, authMessages));
  // TODO(stuartmorgan): Replace this with structured errors, coordinated
  // across all platform implementations, per
  // https://github.com/flutter/flutter/wiki/Contributing-to-Plugins-and-Packages#platform-exception-handling
  // The PlatformExceptions thrown here are for compatibiilty with the
  // previous Java implementation.
  switch (result) {
    case AuthResult.success:
      return true;
    case AuthResult.failure:
      return false;
    case AuthResult.errorAlreadyInProgress:
      throw PlatformException(
          code: 'auth_in_progress', message: 'Authentication in progress');
    case AuthResult.errorNoActivity:
      throw PlatformException(
          code: 'no_activity',
          message: 'local_auth plugin requires a foreground activity');
    case AuthResult.errorNotFragmentActivity:
      throw PlatformException(
          code: 'no_fragment_activity',
          message:
              'local_auth plugin requires activity to be a FragmentActivity.');
    case AuthResult.errorNotAvailable:
      throw PlatformException(
          code: 'NotAvailable',
          message: 'Security credentials not available.');
    case AuthResult.errorNotEnrolled:
      throw PlatformException(
          code: 'NotEnrolled',
          message: 'No Biometrics enrolled on this device.');
    case AuthResult.errorLockedOutTemporarily:
      throw PlatformException(
          code: 'LockedOut',
          message: 'The operation was canceled because the API is locked out '
              'due to too many attempts. This occurs after 5 failed '
              'attempts, and lasts for 30 seconds.');
    case AuthResult.errorLockedOutPermanently:
      throw PlatformException(
          code: 'PermanentlyLockedOut',
          message: 'The operation was canceled because ERROR_LOCKOUT '
              'occurred too many times. Biometric authentication is disabled '
              'until the user unlocks with strong authentication '
              '(PIN/Pattern/Password)');
  }
}