mirror of
https://github.com/Dvorinka/1356.git
synced 2026-06-03 19:42:57 +00:00
small fix, don't worry about it
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../data/repositories/auth_repository.dart';
|
||||
import '../../../data/models/user_model.dart';
|
||||
import '../../../data/services/biometric_service.dart';
|
||||
import '../../../core/services/analytics_service.dart';
|
||||
import '../../../bootstrap/supabase_client.dart';
|
||||
import '../../../core/utils/unit_conversion_utils.dart';
|
||||
import 'package:local_auth/local_auth.dart' as local_auth;
|
||||
|
||||
final authControllerProvider = StateNotifierProvider<AuthController, User?>((ref) {
|
||||
return AuthController(ref.read(authRepositoryProvider));
|
||||
});
|
||||
|
||||
final authRepositoryProvider = Provider<AuthRepository>((ref) {
|
||||
return AuthRepository();
|
||||
return AuthRepository(supabaseClient);
|
||||
});
|
||||
|
||||
class AuthController extends StateNotifier<User?> {
|
||||
final AuthRepository _authRepository;
|
||||
final BiometricService _biometricService = BiometricService();
|
||||
final AnalyticsService _analytics = AnalyticsService();
|
||||
|
||||
AuthController(this._authRepository) : super(null) {
|
||||
@@ -46,8 +51,8 @@ class AuthController extends StateNotifier<User?> {
|
||||
_analytics.logSignIn(method: 'email');
|
||||
}
|
||||
|
||||
Future<void> signUpWithEmail(String email, String password, String username) async {
|
||||
await _authRepository.signUpWithEmail(email, password, username);
|
||||
Future<void> signUpWithEmail(String email, String password, String username, {double? heightCm, double? weightKg, int? age, Gender? gender, HeightUnit? heightUnit, WeightUnit? weightUnit}) async {
|
||||
await _authRepository.signUpWithEmail(email, password, username, heightCm: heightCm, weightKg: weightKg, age: age, gender: gender, heightUnit: heightUnit, weightUnit: weightUnit);
|
||||
_analytics.logSignUp(method: 'email');
|
||||
}
|
||||
|
||||
@@ -77,6 +82,12 @@ class AuthController extends StateNotifier<User?> {
|
||||
String? bio,
|
||||
String? avatarUrl,
|
||||
bool? isPublicProfile,
|
||||
double? heightCm,
|
||||
double? weightKg,
|
||||
int? age,
|
||||
Gender? gender,
|
||||
HeightUnit? heightUnit,
|
||||
WeightUnit? weightUnit,
|
||||
}) async {
|
||||
final updatedFields = <String>[];
|
||||
if (username != null) updatedFields.add('username');
|
||||
@@ -86,12 +97,24 @@ class AuthController extends StateNotifier<User?> {
|
||||
updatedFields.add('visibility');
|
||||
_analytics.logProfileVisibilityChanged(isPublic: isPublicProfile);
|
||||
}
|
||||
if (heightCm != null) updatedFields.add('height');
|
||||
if (weightKg != null) updatedFields.add('weight');
|
||||
if (age != null) updatedFields.add('age');
|
||||
if (gender != null) updatedFields.add('gender');
|
||||
if (heightUnit != null) updatedFields.add('height_unit');
|
||||
if (weightUnit != null) updatedFields.add('weight_unit');
|
||||
|
||||
await _authRepository.updateProfile(
|
||||
username: username,
|
||||
bio: bio,
|
||||
avatarUrl: avatarUrl,
|
||||
isPublicProfile: isPublicProfile,
|
||||
heightCm: heightCm,
|
||||
weightKg: weightKg,
|
||||
age: age,
|
||||
gender: gender,
|
||||
heightUnit: heightUnit,
|
||||
weightUnit: weightUnit,
|
||||
);
|
||||
|
||||
if (updatedFields.isNotEmpty) {
|
||||
@@ -99,6 +122,98 @@ class AuthController extends StateNotifier<User?> {
|
||||
}
|
||||
}
|
||||
|
||||
// Biometric Authentication Methods
|
||||
|
||||
/// Check if biometric authentication is available
|
||||
Future<BiometricAvailability> checkBiometricAvailability() async {
|
||||
return await _biometricService.checkAvailability();
|
||||
}
|
||||
|
||||
/// Check if biometric login is enabled
|
||||
Future<bool> isBiometricEnabled() async {
|
||||
return await _biometricService.isBiometricEnabled();
|
||||
}
|
||||
|
||||
/// Enable biometric login for current user
|
||||
Future<bool> enableBiometric() async {
|
||||
final userId = currentUserId;
|
||||
if (userId == null) return false;
|
||||
|
||||
final success = await _biometricService.enableBiometric(userId);
|
||||
if (success) {
|
||||
_analytics.logEvent('biometric_enabled');
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/// Disable biometric login
|
||||
Future<bool> disableBiometric() async {
|
||||
final success = await _biometricService.disableBiometric();
|
||||
if (success) {
|
||||
_analytics.logEvent('biometric_disabled');
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/// Authenticate with biometrics and sign in
|
||||
Future<bool> signInWithBiometric() async {
|
||||
try {
|
||||
// Check if biometric is enabled
|
||||
final isEnabled = await _biometricService.isBiometricEnabled();
|
||||
if (!isEnabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the stored user ID
|
||||
final biometricUserId = await _biometricService.getBiometricUserId();
|
||||
if (biometricUserId == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Authenticate with biometrics
|
||||
final authenticated = await _biometricService.authenticate(
|
||||
reason: 'Sign in to your 1356 day challenge',
|
||||
localizedReason: 'Use your biometric to quickly access your challenge',
|
||||
);
|
||||
|
||||
if (authenticated) {
|
||||
// Try to restore session for the stored user
|
||||
await _authRepository.refreshSession();
|
||||
|
||||
// Verify the current user matches the stored biometric user
|
||||
final currentUser = _authRepository.currentUser;
|
||||
final currentUserId = currentUser?.id;
|
||||
if (currentUserId == biometricUserId) {
|
||||
_analytics.logSignIn(method: 'biometric');
|
||||
return true;
|
||||
} else {
|
||||
// User mismatch, disable biometric
|
||||
await disableBiometric();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get biometric status message
|
||||
Future<String> getBiometricStatusMessage() async {
|
||||
return await _biometricService.getBiometricStatusMessage();
|
||||
}
|
||||
|
||||
/// Get available biometric types
|
||||
Future<List<local_auth.BiometricType>> getAvailableBiometrics() async {
|
||||
return await _biometricService.getAvailableBiometrics();
|
||||
}
|
||||
|
||||
/// Get primary biometric type
|
||||
Future<local_auth.BiometricType?> getPrimaryBiometricType() async {
|
||||
return await _biometricService.getPrimaryBiometricType();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_authRepository.dispose();
|
||||
|
||||
Reference in New Issue
Block a user