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, 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 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);
}
}