login auth

This commit is contained in:
2026-07-13 12:21:56 +05:30
parent 523609796e
commit 842c2307d0
4 changed files with 40 additions and 67 deletions

View File

@@ -133,74 +133,44 @@ class AppState extends ChangeNotifier {
pendingFirstName = firstName;
pendingEmail = email;
isResettingPin = resetPin;
try {
if (kIsWeb) {
debugPrint('⚠️ RUNNING ON WEB: Bypassing Firebase OTP (Mocking success)');
Future.delayed(const Duration(seconds: 1), () {
verificationId = 'mock_verification_id_$fullPhone';
onSuccess();
});
return;
}
debugPrint('====================================');
debugPrint('🚀 INITIATING LIVE FIREBASE OTP FOR: $fullPhone');
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: fullPhone,
verificationCompleted: (PhoneAuthCredential credential) async {
debugPrint('✅ VERIFICATION COMPLETED AUTOMATICALLY');
try {
await FirebaseAuth.instance.signInWithCredential(credential);
} catch (e) {
debugPrint('❌ ERROR SIGNING IN: $e');
onError(e.toString());
}
},
verificationFailed: (FirebaseAuthException e) {
debugPrint('❌ VERIFICATION FAILED: ${e.code} - ${e.message}');
onError(e.message ?? 'Verification failed');
},
codeSent: (String vId, int? resendToken) {
debugPrint('✅ CODE SUCCESSFULLY SENT. Verification ID: $vId');
verificationId = vId;
onSuccess();
},
codeAutoRetrievalTimeout: (String vId) {
debugPrint('⚠️ AUTO RETRIEVAL TIMEOUT. Verification ID: $vId');
verificationId = vId;
},
if (email == null || email.isEmpty) {
onError('Email address is required for verification.');
return;
}
try {
final res = await http.post(
Uri.parse(ApiConstants.sendEmailOtp),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({"email": email, "phone": fullPhone}),
);
if (res.statusCode >= 200 && res.statusCode < 300) {
onSuccess();
} else {
final body = jsonDecode(res.body);
onError(body['message'] ?? 'Failed to send verification code.');
}
} catch (e) {
debugPrint('💥 FIREBASE OTP CRASHED COMPLETELY: $e');
onError('Firebase OTP Error: $e');
onError('Error: $e');
}
}
Future<void> verifyOtp(String smsCode, {required Function onSuccess, required Function(String) onError}) async {
if (verificationId == null) {
onError('Verification ID is missing. Please resend OTP.');
return;
}
try {
if (kIsWeb) {
debugPrint('⚠️ RUNNING ON WEB: Bypassing Firebase OTP verification (Mocking success)');
Future.delayed(const Duration(seconds: 1), () {
if (smsCode.length == 6) {
onSuccess();
} else {
onError('Invalid mock OTP code (must be 6 digits).');
}
});
return;
}
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId!,
smsCode: smsCode,
final res = await http.post(
Uri.parse(ApiConstants.verifyEmailOtp),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({"email": pendingEmail, "otp": smsCode}),
);
await FirebaseAuth.instance.signInWithCredential(credential);
onSuccess();
if (res.statusCode >= 200 && res.statusCode < 300) {
onSuccess();
} else {
final body = jsonDecode(res.body);
onError(body['message'] ?? 'Invalid verification code.');
}
} catch (e) {
onError('Error: $e');
}