Reputation: 41
I'm building a Flutter app and I'd like to add support for biometric authentication (e.g. fingerprint or face recognition) to improve security and convenience for my users. After doing some research, I've decided to use the 'local_auth' package to handle the biometric authentication process.
However, I'm not sure how to integrate the package into my app or how to handle the authentication flow. Can someone provide guidance on how to implement biometric authentication using the 'local_auth' package in a Flutter app? Are there any code examples or tutorials that I can refer to?
Here's the relevant code that I've already written to handle user authentication with email and password:
import 'package:firebase_auth/firebase_auth.dart';
class AuthenticationService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<UserCredential> signIn({String email, String password}) async {
return await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
}
Future<UserCredential> signUp({String email, String password}) async {
return await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
}
Future<void> signOut() async {
return await _firebaseAuth.signOut();
}
Stream<User> get currentUser {
return _firebaseAuth.authStateChanges();
}
}
Upvotes: 0
Views: 4609
Reputation: 175
You can save the user and password in shared_preferences
when the user activates biometric permissions after login. and when you log in you call what you have stored in sharedpreferences if user and password is not null you read biometrics and if it works you just need to enter user and password you have saved to hit in firebase.
import 'package:firebase_auth/firebase_auth.dart';
class AuthenticationService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<UserCredential> signIn({String email, String password}) async {
return await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
}
Future<UserCredential> signUp({String email, String password}) async {
return await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
}
Future<void> signOut() async {
return await _firebaseAuth.signOut();
}
Stream<User> get currentUser {
return _firebaseAuth.authStateChanges();
}
}
class AuthenticationService {
final LocalAuthentication auth = LocalAuthentication();
void authchack(context) async {
List<BiometricType> availableBiometrics =
await auth.getAvailableBiometrics();
if (Platform.isIOS) {
if (availableBiometrics.contains(BiometricType.face)) {
startBioMetricAuth(
context, "Use Face ID");
} else if (availableBiometrics.contains(BiometricType.fingerprint)) {
startBioMetricAuth(
context, "Use Touch ID");
}
} else {
startBioMetricAuth(
context, "Use Fingerprint");
}
}
void startBioMetricAuth(context, String message) async {
try {
bool didAuthenticate = await auth.authenticate(localizedReason: message);
if (didAuthenticate) {
// your login function
} else {
showSnackBar(context, 'failed to authenticate');
}
} on PlatformException {
if (kDebugMode) {
print("Error!");
}
}
}
}
Upvotes: 0