|
| 1 | +# local_auth |
| 2 | + |
| 3 | +This Flutter plugin provides means to perform local, on-device authentication of |
| 4 | +the user. |
| 5 | + |
| 6 | +This means referring to biometric authentication on iOS (Touch ID or lock code) |
| 7 | +and the fingerprint APIs on Android (introduced in Android 6.0). |
| 8 | + |
| 9 | +## Usage in Dart |
| 10 | + |
| 11 | +Import the relevant file: |
| 12 | + |
| 13 | +```dart |
| 14 | +import 'package:local_auth/local_auth.dart'; |
| 15 | +``` |
| 16 | + |
| 17 | +We have default dialogs with an 'OK' button to show authentication error |
| 18 | +messages for the following 2 cases: |
| 19 | + |
| 20 | +1. Passcode/PIN/Pattern Not Set. The user has not yet configured a passcode on |
| 21 | + iOS or PIN/pattern on Android. |
| 22 | +2. Touch ID/Fingerprint Not Enrolled. The user has not enrolled any |
| 23 | + fingerprints on the device. |
| 24 | + |
| 25 | +Which means, if there's no fingerprint on the user's device, a dialog with |
| 26 | +instructions will pop up to let the user set up fingerprint. If the user clicks |
| 27 | +'OK' button, it will return 'false'. |
| 28 | + |
| 29 | +Use the exported APIs to trigger local authentication with default dialogs: |
| 30 | + |
| 31 | +```dart |
| 32 | +LocalAuthentication localAuth = new LocalAuthentication(); |
| 33 | +bool didAuthenticate = |
| 34 | + await localAuth.authenticateWithBiometrics( |
| 35 | + localizedReason: 'Please authenticate to show account balance'); |
| 36 | +``` |
| 37 | + |
| 38 | +If you don't want to use the default dialogs, call this API with |
| 39 | +'useErrorDialogs = false'. In this case, it will throw the error message back |
| 40 | +and you need to handle them in your dart code: |
| 41 | + |
| 42 | +```dart |
| 43 | +bool didAuthenticate = |
| 44 | + await localAuth.authenticateWithBiometrics( |
| 45 | + localizedReason: 'Please authenticate to show account balance', |
| 46 | + useErrorDialogs: false); |
| 47 | +``` |
| 48 | + |
| 49 | +You can use our default dialog messages, or you can use your own messages by |
| 50 | +passing in IOSAuthMessages and AndroidAuthMessages: |
| 51 | + |
| 52 | +```dart |
| 53 | +import 'package:local_auth/auth_strings.dart'; |
| 54 | +
|
| 55 | +const iosStrings = const IOSAuthMessages( |
| 56 | + cancelButton: 'cancel', |
| 57 | + goToSettingsButton: 'settings', |
| 58 | + goToSettingsDescription: 'Please set up your Touch ID.', |
| 59 | + lockOut: 'Please reenable your Touch ID'); |
| 60 | +await localAuth.authenticateWithBiometrics( |
| 61 | + localizedReason: 'Please authenticate to show account balance', |
| 62 | + useErrorDialogs: false, |
| 63 | + iOSAuthStrings: iosStrings); |
| 64 | +
|
| 65 | +``` |
| 66 | + |
| 67 | +### Exceptions |
| 68 | + |
| 69 | +There are 4 types of exceptions: PasscodeNotSet, NotEnrolled, NotAvailable and |
| 70 | +OtherOperatingSystem. They are wrapped in LocalAuthenticationError class. You can |
| 71 | +catch the exception and handle them by different types. For example: |
| 72 | + |
| 73 | +```dart |
| 74 | +import 'package:flutter/services.dart'; |
| 75 | +import 'package:local_auth/error_codes.dart' as auth_error; |
| 76 | +
|
| 77 | +try { |
| 78 | + bool didAuthenticate = await local_auth.authenticateWithBiometrics( |
| 79 | + localizedReason: 'Please authenticate to show account balance'); |
| 80 | +} on PlatformException catch (e) { |
| 81 | + if (e.code == auth_error.notAvailable) { |
| 82 | + // Handle this exception here. |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Android integration |
| 88 | + |
| 89 | +Update your project's `AndroidManifest.xml` file to include the |
| 90 | +`USE_FINGERPRINT` permissions: |
| 91 | + |
| 92 | +``` |
| 93 | +<manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 94 | + package="com.example.app"> |
| 95 | + <uses-permission android:name="android.permission.USE_FINGERPRINT"/> |
| 96 | +<manifest> |
| 97 | +``` |
| 98 | + |
| 99 | +## Getting Started |
| 100 | + |
| 101 | +For help getting started with Flutter, view our online |
| 102 | +[documentation](http://flutter.io/). |
| 103 | + |
| 104 | +For help on editing plugin code, view the [documentation](https://flutter.io/platform-plugins/#edit-code). |
0 commit comments