فحص آليّ لنقاط docs/38 مقابل الكود كشف ثغرات لم تظهر بالقراءة. ## تسجيل السائق — أخطر ثغرة لم يكن موجوداً أصلاً: سائق جديد يسجّل دخوله ثم يقف. بُنيت features/onboarding كاملة — تقديم ← ملف ← مركبة ← وثائق ← انتظار الاعتماد. - الخطوة تُشتقّ من حالة الخادم لا من تقدّم محلّي: سائق يعيد تثبيت التطبيق يعود إلى حيث وقف لا إلى البداية - الاعتماد يقع على الخادم تلقائياً حين تكتمل الوثائق؛ التطبيق لا يعتمد أحداً - عند الاعتماد خروج إجباري: الدور يتغيّر على الخادم والتوكن القديم يحمل القديم، فتفشل نقاط السائق بـ403 (مصيدة docs/38 §5) - بوابة توجيه: مستخدم دوره ليس driver يُحجز في /onboarding - الوثائق تُصوَّر بالكاميرا لا من المعرض: أصعب تزويراً ## الدردشة والكوبون - Features.chat كان مفعّلاً بلا ميزة. features/chat باستطلاع كل خمس ثوان — قائمة أحداث الخادم لا تتضمّن الرسائل (docs/38 §9)، فالاستطلاع قيد خادم لا اختيار تصميمي - الكوبون: حقل في ورقة التأكيد خلف طبقتَي الميزات، يُقيَّم على الخادم ## Features.calls أُطفئ صراحةً الخادم يدعم WebRTC والتطبيق لا. عَلَم مفعّل بلا ميزة كذبٌ على القارئ التالي. ## بنية - ApiClient.upload للرفع متعدّد الأجزاء - AuthFailure → ApiFailure في core/api: يخدم المصادقة والملف والتسجيل - tool/sync_from_rider.sh: المزامنة اليدوية بين التوأمين انكسرت أربع مرات، فصارت سكربتاً واحداً يعرّف ما يملكه كل تطبيق flutter analyze نظيف · الراكب 101 ملف/7,809 سطر · السائق 110 ملف/8,116 سطر. فحوص آلية: صفر استيراد بين ميزتين · صفر عَلَم مفعّل بلا ميزة · كل نقاط العقد المخصّصة للتطبيقين مستهلكة. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
177 lines
5.6 KiB
Dart
177 lines
5.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../../../core/api/api_exception.dart';
|
|
|
|
import '../../../core/api/api_failure.dart';
|
|
|
|
import '../../../core/config.dart';
|
|
|
|
import '../data/auth_repository.dart';
|
|
|
|
import 'login_state.dart';
|
|
|
|
import 'phone_error.dart';
|
|
|
|
/// تدفّق الدخول كاملاً: الشروط ← إذن الموقع ← الهاتف ← الرمز.
|
|
///
|
|
/// الـCubit لا يعرف Flutter: لا `BuildContext` ولا تنقّل ولا `SnackBar`
|
|
/// (docs/23 §3). يُصدر حالة، والواجهة تتصرّف.
|
|
class LoginCubit extends Cubit<LoginState> {
|
|
LoginCubit(this._repo, this._prefs) : super(const LoginState()) {
|
|
_resolveInitialStep();
|
|
}
|
|
|
|
final AuthRepository _repo;
|
|
final SharedPreferences _prefs;
|
|
|
|
static const _kAgreed = 'auth.agreed_terms';
|
|
|
|
Timer? _resendTimer;
|
|
|
|
@override
|
|
Future<void> close() {
|
|
_resendTimer?.cancel();
|
|
return super.close();
|
|
}
|
|
|
|
// ── البوابتان ──────────────────────────────────────────────────────────
|
|
|
|
void _resolveInitialStep() {
|
|
if (_prefs.getBool(_kAgreed) != true) {
|
|
emit(state.copyWith(step: LoginStep.agreement));
|
|
return;
|
|
}
|
|
emit(state.copyWith(step: LoginStep.permission, agreed: true));
|
|
unawaited(checkLocationPermission());
|
|
}
|
|
|
|
void toggleAgreement(bool value) => emit(state.copyWith(agreed: value));
|
|
|
|
Future<void> acceptAgreement() async {
|
|
if (!state.agreed) return;
|
|
await _prefs.setBool(_kAgreed, true);
|
|
emit(state.copyWith(step: LoginStep.permission));
|
|
await checkLocationPermission();
|
|
}
|
|
|
|
/// تُستدعى أيضاً عند **العودة من إعدادات النظام**: بلا إعادة الفحص تبقى
|
|
/// الشاشة عالقة بعد أن يمنح المستخدم الإذن يدوياً (حالة حافة من docs/39 §2).
|
|
Future<void> checkLocationPermission() async {
|
|
final status = await Permission.locationWhenInUse.status;
|
|
if (status.isGranted || status.isLimited) {
|
|
emit(state.copyWith(step: LoginStep.phone));
|
|
return;
|
|
}
|
|
emit(state.copyWith(
|
|
permissionPermanentlyDenied: status.isPermanentlyDenied,
|
|
));
|
|
}
|
|
|
|
Future<void> requestLocationPermission() async {
|
|
final status = await Permission.locationWhenInUse.request();
|
|
if (status.isGranted || status.isLimited) {
|
|
emit(state.copyWith(step: LoginStep.phone));
|
|
return;
|
|
}
|
|
emit(state.copyWith(
|
|
permissionPermanentlyDenied: status.isPermanentlyDenied,
|
|
));
|
|
}
|
|
|
|
Future<void> openSystemSettings() => openAppSettings();
|
|
|
|
// ── الهاتف ─────────────────────────────────────────────────────────────
|
|
|
|
void phoneChanged(String value) {
|
|
emit(state.copyWith(
|
|
phone: value,
|
|
clearPhoneError: true,
|
|
clearFailure: true,
|
|
));
|
|
}
|
|
|
|
Future<void> submitPhone() async {
|
|
final error = validatePhone(state.phone);
|
|
if (error != null) {
|
|
emit(state.copyWith(phoneError: error));
|
|
return;
|
|
}
|
|
emit(state.copyWith(status: LoginStatus.submitting, clearFailure: true));
|
|
try {
|
|
await _repo.sendOtp(state.phone.trim());
|
|
emit(state.copyWith(status: LoginStatus.idle, step: LoginStep.otp));
|
|
_startResendCountdown();
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toApiFailure(),
|
|
));
|
|
}
|
|
}
|
|
|
|
void editPhone() {
|
|
_resendTimer?.cancel();
|
|
emit(state.copyWith(
|
|
step: LoginStep.phone,
|
|
status: LoginStatus.idle,
|
|
resendIn: 0,
|
|
clearFailure: true,
|
|
));
|
|
}
|
|
|
|
// ── الرمز ──────────────────────────────────────────────────────────────
|
|
|
|
Future<void> resendOtp() async {
|
|
if (!state.canResend) return;
|
|
emit(state.copyWith(status: LoginStatus.submitting, clearFailure: true));
|
|
try {
|
|
await _repo.sendOtp(state.phone.trim());
|
|
emit(state.copyWith(status: LoginStatus.idle));
|
|
_startResendCountdown();
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toApiFailure(),
|
|
));
|
|
// حتى عند الرفض بحدّ المعدّل نبدأ العدّاد — وإلا ضغط المستخدم مجدداً
|
|
// فوراً وعمّق الحظر.
|
|
_startResendCountdown();
|
|
}
|
|
}
|
|
|
|
Future<void> submitOtp(String code) async {
|
|
if (code.length != AppConfig.otpLength) return;
|
|
emit(state.copyWith(status: LoginStatus.submitting, clearFailure: true));
|
|
try {
|
|
final user = await _repo.verifyOtp(phone: state.phone.trim(), code: code);
|
|
_resendTimer?.cancel();
|
|
emit(state.copyWith(
|
|
status: LoginStatus.success,
|
|
step: LoginStep.done,
|
|
user: user,
|
|
));
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toApiFailure(),
|
|
));
|
|
}
|
|
}
|
|
|
|
void _startResendCountdown() {
|
|
_resendTimer?.cancel();
|
|
emit(state.copyWith(resendIn: AppConfig.otpResendCooldown.inSeconds));
|
|
_resendTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
final next = state.resendIn - 1;
|
|
if (next <= 0) timer.cancel();
|
|
emit(state.copyWith(resendIn: next < 0 ? 0 : next));
|
|
});
|
|
}
|
|
}
|