Sub topic 11
Adding a Biometric Check with a Separate Package
This chapter assumes you know basic Dart, async and await, Flutter packages, and the token repository from the previous chapter. You will add local_auth, a package that asks the operating system to verify a fingerprint, face, or other supported biometric.
What the biometric check does
flutter_secure_storage protects stored values with platform security services. It does not automatically ask for a fingerprint every time your code reads a value. A biometric check is a separate step that your application controls.
A common flow is:
- Check whether the device supports local authentication.
- Ask the user to authenticate.
- Read the secret only after authentication succeeds.
- Treat cancellation or failure as a denied operation.
Biometric authentication proves that the person is currently present. It does not prove that your server issued the token, and it does not replace server-side authorization.
Install and configure `local_auth`
Add the package:
flutter pub add local_authThe package also needs native configuration. On Android, your activity must use FlutterFragmentActivity, and the manifest needs the biometric permission:
<uses-permission android:name="android.permission.USE_BIOMETRIC" />On iOS, add NSFaceIDUsageDescription to ios/Runner/Info.plist if your app supports Face ID. Use a clear explanation, such as “Use Face ID to unlock your account.”
Check the package documentation for the version you install. Native setup and API details can change between releases.
Put the check at the access boundary
Keep the biometric prompt close to the operation it protects. The following service asks for authentication before reading a refresh token.
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:local_auth/local_auth.dart';
class BiometricTokenReader {
BiometricTokenReader({
LocalAuthentication? authentication,
FlutterSecureStorage? storage,
}) : _authentication = authentication ?? LocalAuthentication(),
_storage = storage ?? const FlutterSecureStorage();
static const _refreshTokenKey = 'refresh_token';
final LocalAuthentication _authentication;
final FlutterSecureStorage _storage;
Future<String?> readRefreshToken() async {
try {
final supported = await _authentication.isDeviceSupported();
if (!supported) {
return null;
}
final authenticated = await _authentication.authenticate(
localizedReason: 'Authenticate to access your account',
options: AuthenticationOptions(
biometricOnly: true,
stickyAuth: true,
),
);
if (!authenticated) {
return null;
}
return await _storage.read(key: _refreshTokenKey);
} on PlatformException {
return null;
}
}
}biometricOnly: true prevents the operating system from accepting a device PIN or password as a fallback. Set it to false when your security policy allows device credentials. stickyAuth helps the authentication flow resume after an interruption, such as leaving the app temporarily.
Do not prompt on every screen rebuild. Call this service in response to a deliberate action, such as opening a locked account page or starting an export.
Choose a fallback deliberately
Devices may lack biometric hardware, have no enrolled biometric, or deny permission. Decide what each case means before writing the feature:
| Situation | Possible application behavior |
|---|---|
| No supported authentication | Require a normal sign-in |
| User cancels | Stay on the locked screen |
| Authentication error | Show a retry action |
| Authentication succeeds | Read and use the secret |
Avoid silently returning a token when authentication fails. If biometric protection is required by the product, a fallback to unrestricted storage access defeats that requirement.
Exercise
Why should the biometric prompt happen before the call to storage.read?
Show answer
Secure storage protects the value at rest, but it does not automatically enforce a user prompt. Checking first makes the biometric result an explicit condition for reading the secret.