authenticate method
- required String localizedReason,
- required Iterable<
AuthMessages> authMessages, - AuthenticationOptions options = const AuthenticationOptions(),
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. Returns false if
the authentication completes, but the user failed the challenge with no
further effects. Platform implementations should throw a
LocalAuthException
for any other outcome, such as errors, cancelation,
or lockout. This may mean that for some platforms, the implementation will
never return false (e.g., if the only standard outcomes are success,
cancelation, or temporary lockout due to too many retries).
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.
Implementation
@override
Future<bool> authenticate({
required String localizedReason,
required Iterable<AuthMessages> authMessages,
AuthenticationOptions options = const AuthenticationOptions(),
}) async {
assert(localizedReason.isNotEmpty);
final AuthResultDetails resultDetails = await _api.authenticate(
AuthOptions(
biometricOnly: options.biometricOnly,
sticky: options.stickyAuth,
useErrorDialogs: options.useErrorDialogs),
_useMacOSAuthMessages
? _pigeonStringsFromMacOSAuthMessages(localizedReason, authMessages)
: _pigeonStringsFromiOSAuthMessages(localizedReason, authMessages));
// TODO(stuartmorgan): Replace this with structured errors, coordinated
// across all platform implementations, per
// https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#platform-exception-handling
// The PlatformExceptions thrown here are for compatibiilty with the
// previous Objective-C implementation.
switch (resultDetails.result) {
case AuthResult.success:
return true;
case AuthResult.failure:
return false;
case AuthResult.errorNotAvailable:
throw PlatformException(
code: 'NotAvailable',
message: resultDetails.errorMessage,
details: resultDetails.errorDetails);
case AuthResult.errorNotEnrolled:
throw PlatformException(
code: 'NotEnrolled',
message: resultDetails.errorMessage,
details: resultDetails.errorDetails);
case AuthResult.errorPasscodeNotSet:
throw PlatformException(
code: 'PasscodeNotSet',
message: resultDetails.errorMessage,
details: resultDetails.errorDetails);
}
}