@@ -12,14 +12,11 @@ import 'dart:async';
12
12
13
13
import 'package:flutter/foundation.dart' show visibleForTesting;
14
14
import 'package:flutter/services.dart' ;
15
+ import 'package:local_auth_platform_interface/local_auth_platform_interface.dart' ;
15
16
import 'package:platform/platform.dart' ;
16
-
17
17
import 'auth_strings.dart' ;
18
- import 'error_codes.dart' ;
19
-
20
- enum BiometricType { face, fingerprint, iris }
21
18
22
- const MethodChannel _channel = MethodChannel ( 'plugins.flutter.io/local_auth' ) ;
19
+ export 'package:local_auth_platform_interface/types/biometric_type.dart' ;
23
20
24
21
Platform _platform = const LocalPlatform ();
25
22
@@ -31,7 +28,7 @@ void setMockPathProviderPlatform(Platform platform) {
31
28
/// A Flutter plugin for authenticating the user identity locally.
32
29
class LocalAuthentication {
33
30
/// The `authenticateWithBiometrics` method has been deprecated.
34
- /// Use `authenticate` with `biometricOnly: true` instead
31
+ /// Use `authenticate` with `biometricOnly: true` instead.
35
32
@Deprecated ('Use `authenticate` with `biometricOnly: true` instead' )
36
33
Future <bool > authenticateWithBiometrics ({
37
34
required String localizedReason,
@@ -41,21 +38,21 @@ class LocalAuthentication {
41
38
IOSAuthMessages iOSAuthStrings = const IOSAuthMessages (),
42
39
bool sensitiveTransaction = true ,
43
40
}) =>
44
- authenticate (
41
+ LocalAuthPlatform .instance. authenticate (
45
42
localizedReason: localizedReason,
46
- useErrorDialogs: useErrorDialogs,
47
- stickyAuth: stickyAuth,
48
- androidAuthStrings: androidAuthStrings,
49
- iOSAuthStrings: iOSAuthStrings,
50
- sensitiveTransaction: sensitiveTransaction,
51
- biometricOnly: true ,
43
+ authMessages: < AuthMessages > [iOSAuthStrings, androidAuthStrings],
44
+ options: AuthenticationOptions (
45
+ useErrorDialogs: useErrorDialogs,
46
+ stickyAuth: stickyAuth,
47
+ sensitiveTransaction: sensitiveTransaction,
48
+ biometricOnly: true ,
49
+ ),
52
50
);
53
51
54
52
/// Authenticates the user with biometrics available on the device while also
55
53
/// allowing the user to use device authentication - pin, pattern, passcode.
56
54
///
57
- /// Returns a [Future] holding true, if the user successfully authenticated,
58
- /// false otherwise.
55
+ /// Returns true, if the user successfully authenticated, false otherwise.
59
56
///
60
57
/// [localizedReason] is the message to show to user while prompting them
61
58
/// for authentication. This is typically along the lines of: 'Please scan
@@ -99,29 +96,17 @@ class LocalAuthentication {
99
96
IOSAuthMessages iOSAuthStrings = const IOSAuthMessages (),
100
97
bool sensitiveTransaction = true ,
101
98
bool biometricOnly = false ,
102
- }) async {
103
- assert (localizedReason.isNotEmpty);
104
-
105
- final Map <String , Object > args = < String , Object > {
106
- 'localizedReason' : localizedReason,
107
- 'useErrorDialogs' : useErrorDialogs,
108
- 'stickyAuth' : stickyAuth,
109
- 'sensitiveTransaction' : sensitiveTransaction,
110
- 'biometricOnly' : biometricOnly,
111
- };
112
- if (_platform.isIOS) {
113
- args.addAll (iOSAuthStrings.args);
114
- } else if (_platform.isAndroid) {
115
- args.addAll (androidAuthStrings.args);
116
- } else {
117
- throw PlatformException (
118
- code: otherOperatingSystem,
119
- message: 'Local authentication does not support non-Android/iOS '
120
- 'operating systems.' ,
121
- details: 'Your operating system is ${_platform .operatingSystem }' ,
122
- );
123
- }
124
- return (await _channel.invokeMethod <bool >('authenticate' , args)) ?? false ;
99
+ }) {
100
+ return LocalAuthPlatform .instance.authenticate (
101
+ localizedReason: localizedReason,
102
+ authMessages: < AuthMessages > [iOSAuthStrings, androidAuthStrings],
103
+ options: AuthenticationOptions (
104
+ useErrorDialogs: useErrorDialogs,
105
+ stickyAuth: stickyAuth,
106
+ sensitiveTransaction: sensitiveTransaction,
107
+ biometricOnly: biometricOnly,
108
+ ),
109
+ );
125
110
}
126
111
127
112
/// Returns true if auth was cancelled successfully.
@@ -131,52 +116,30 @@ class LocalAuthentication {
131
116
/// Returns [Future] bool true or false:
132
117
Future <bool > stopAuthentication () async {
133
118
if (_platform.isAndroid) {
134
- return await _channel. invokeMethod < bool >( ' stopAuthentication' ) ?? false ;
119
+ return LocalAuthPlatform .instance. stopAuthentication () ;
135
120
}
136
121
return true ;
137
122
}
138
123
139
124
/// Returns true if device is capable of checking biometrics
140
125
///
141
126
/// Returns a [Future] bool true or false:
142
- Future <bool > get canCheckBiometrics async =>
143
- (await _channel.invokeListMethod <String >('getAvailableBiometrics' ))!
144
- .isNotEmpty;
127
+ Future <bool > get canCheckBiometrics =>
128
+ LocalAuthPlatform .instance.deviceSupportsBiometrics ();
145
129
146
130
/// Returns true if device is capable of checking biometrics or is able to
147
131
/// fail over to device credentials.
148
132
///
149
133
/// Returns a [Future] bool true or false:
150
134
Future <bool > isDeviceSupported () async =>
151
- ( await _channel. invokeMethod < bool >( ' isDeviceSupported' )) ?? false ;
135
+ LocalAuthPlatform .instance. isDeviceSupported () ;
152
136
153
137
/// Returns a list of enrolled biometrics
154
138
///
155
139
/// Returns a [Future] List<BiometricType> with the following possibilities:
156
140
/// - BiometricType.face
157
141
/// - BiometricType.fingerprint
158
142
/// - BiometricType.iris (not yet implemented)
159
- Future <List <BiometricType >> getAvailableBiometrics () async {
160
- final List <String > result = (await _channel.invokeListMethod <String >(
161
- 'getAvailableBiometrics' ,
162
- )) ??
163
- < String > [];
164
- final List <BiometricType > biometrics = < BiometricType > [];
165
- for (final String value in result) {
166
- switch (value) {
167
- case 'face' :
168
- biometrics.add (BiometricType .face);
169
- break ;
170
- case 'fingerprint' :
171
- biometrics.add (BiometricType .fingerprint);
172
- break ;
173
- case 'iris' :
174
- biometrics.add (BiometricType .iris);
175
- break ;
176
- case 'undefined' :
177
- break ;
178
- }
179
- }
180
- return biometrics;
181
- }
143
+ Future <List <BiometricType >> getAvailableBiometrics () =>
144
+ LocalAuthPlatform .instance.getEnrolledBiometrics ();
182
145
}
0 commit comments