mirror of
https://github.com/we-promise/sure.git
synced 2026-05-12 23:25:00 +00:00
feat(mobile): Add biometric lock for app resume (#1474)
* Feature: Biometric lock for app resume User enables "Biometric Lock" in Settings → prompted to verify fingerprint/face first. User backgrounds the app → _isLocked = true. User returns → lock screen appears, auto-triggers biometric prompt. Success → app unlocks, state preserved underneath. Can retry or log out as fallback. Add USE_BIOMETRIC permission to AndroidManifest. * Fix: Remove duplicate local_auth entry in pubspec.lock and add NSFaceIDUsageDescription to iOS Info.plist * fix(mobile) : Remove duplicate local auth files first * fix(mobile): keep MainNavigationScreen in the Stack, let the lock screen float above, no unmounting * Updtae: Swap out Flutter Activity for FlutterFragmentActivity that extends the lock scan feature * fix(mobile): address biometric lock PR review feedback * fix(mobile): only require biometric auth when enabling lock, not disabling Prevents users from getting locked out if biometrics start failing — they can now disable the lock without needing to pass biometric auth. * fix(mobile): add missing closing brace in setBiometricEnabled --------- Signed-off-by: Tristan Katana <50181095+felixmuinde@users.noreply.github.com>
This commit is contained in:
99
mobile/lib/screens/biometric_lock_screen.dart
Normal file
99
mobile/lib/screens/biometric_lock_screen.dart
Normal file
@@ -0,0 +1,99 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../services/biometric_service.dart';
|
||||
|
||||
class BiometricLockScreen extends StatefulWidget {
|
||||
const BiometricLockScreen({
|
||||
super.key,
|
||||
required this.onUnlocked,
|
||||
this.onLogout,
|
||||
});
|
||||
|
||||
final VoidCallback onUnlocked;
|
||||
final VoidCallback? onLogout;
|
||||
|
||||
@override
|
||||
State<BiometricLockScreen> createState() => _BiometricLockScreenState();
|
||||
}
|
||||
|
||||
class _BiometricLockScreenState extends State<BiometricLockScreen> {
|
||||
bool _isAuthenticating = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Auto-trigger biometric prompt on first show.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _authenticate());
|
||||
}
|
||||
|
||||
Future<void> _authenticate() async {
|
||||
if (!mounted || _isAuthenticating) return;
|
||||
setState(() => _isAuthenticating = true);
|
||||
|
||||
final success = await BiometricService.instance.authenticate();
|
||||
|
||||
if (!mounted) return;
|
||||
setState(() => _isAuthenticating = false);
|
||||
|
||||
if (success) {
|
||||
widget.onUnlocked();
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Authentication failed. Tap Unlock to try again.')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.lock_outline,
|
||||
size: 72,
|
||||
color: colorScheme.primary,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
'App Locked',
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Authenticate to continue',
|
||||
style: TextStyle(color: colorScheme.onSurfaceVariant),
|
||||
),
|
||||
const SizedBox(height: 40),
|
||||
FilledButton.icon(
|
||||
onPressed: _isAuthenticating ? null : _authenticate,
|
||||
icon: const Icon(Icons.fingerprint),
|
||||
label: Text(_isAuthenticating ? 'Authenticating…' : 'Unlock'),
|
||||
style: FilledButton.styleFrom(
|
||||
minimumSize: const Size(200, 50),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (widget.onLogout != null) ...[
|
||||
const SizedBox(height: 16),
|
||||
TextButton(
|
||||
onPressed: widget.onLogout,
|
||||
child: const Text('Log out'),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import '../providers/categories_provider.dart';
|
||||
import '../providers/theme_provider.dart';
|
||||
import '../services/offline_storage_service.dart';
|
||||
import '../services/log_service.dart';
|
||||
import '../services/biometric_service.dart';
|
||||
import '../services/preferences_service.dart';
|
||||
import '../services/user_service.dart';
|
||||
import 'log_viewer_screen.dart';
|
||||
@@ -23,12 +24,53 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
String? _appVersion;
|
||||
bool _isResettingAccount = false;
|
||||
bool _isDeletingAccount = false;
|
||||
bool _biometricSupported = false;
|
||||
bool _biometricEnabled = false;
|
||||
bool _isTogglingBiometric = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadPreferences();
|
||||
_loadAppVersion();
|
||||
_loadBiometricState();
|
||||
}
|
||||
|
||||
Future<void> _loadBiometricState() async {
|
||||
final supported = await BiometricService.instance.isDeviceSupported();
|
||||
final enabled = await PreferencesService.instance.getBiometricEnabled();
|
||||
if (!supported && enabled) {
|
||||
await PreferencesService.instance.setBiometricEnabled(false);
|
||||
}
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_biometricSupported = supported;
|
||||
_biometricEnabled = supported && enabled;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _toggleBiometric(bool value) async {
|
||||
if (_isTogglingBiometric) return;
|
||||
setState(() => _isTogglingBiometric = true);
|
||||
try {
|
||||
if (value) {
|
||||
final success = await BiometricService.instance.authenticate(
|
||||
reason: 'Verify biometric to enable app lock',
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (!success) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Biometric authentication failed.')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
await PreferencesService.instance.setBiometricEnabled(value);
|
||||
if (mounted) setState(() => _biometricEnabled = value);
|
||||
} finally {
|
||||
if (mounted) setState(() => _isTogglingBiometric = false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadAppVersion() async {
|
||||
@@ -461,6 +503,30 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
onTap: () => _handleClearLocalData(context),
|
||||
),
|
||||
|
||||
if (_biometricSupported) ...[
|
||||
const Divider(),
|
||||
|
||||
const Padding(
|
||||
padding: EdgeInsets.fromLTRB(16, 16, 16, 8),
|
||||
child: Text(
|
||||
'Security',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SwitchListTile(
|
||||
secondary: const Icon(Icons.fingerprint),
|
||||
title: const Text('Biometric Lock'),
|
||||
subtitle: const Text('Require biometric authentication when resuming the app'),
|
||||
value: _biometricEnabled,
|
||||
onChanged: _isTogglingBiometric ? null : _toggleBiometric,
|
||||
),
|
||||
],
|
||||
|
||||
const Divider(),
|
||||
|
||||
// Danger Zone Section
|
||||
|
||||
Reference in New Issue
Block a user