feat: N1d/N2/N3 + قانون فلاتر وإعادة ضبط تطبيق السائق
الباك إند: - تعيين أول أدمن صار يُنشئ المستخدم إن لم يوجد — كان يشترط دخولاً سابقاً، وهي بيضة ودجاجة تمنع الدخول إلى لوحة أي مستأجر جديد أصلاً. - OTP_DEV_MODE كان يفشل مفتوحاً (`!== 'false'`): غياب المتغيّر أو خطأ مطبعي يترك الإنتاج برمز ثابت يفتح كل حساب، ويحرس السحب المالي كذلك. صار `=== 'true'` في الإعداد وفي قارئَيه، مع تحذير عند الإقلاع. - N2: التحقّق من اللوغو عند الرفع (PNG/JPEG · 512+ · مربّع · سقف 5MB) عبر قراءة الترويسة بلا اعتمادية — بدل اكتشاف أيقونة ممطوطة بعد النشر. N3: السكربت يولّد build_config.dart بأعلام const (طبقات docs/22 §1.5)، ويضبط bundle IDs واسم التطبيق والأيقونات/splash، و--build يشغّل Shorebird. فلاتر: - docs/23: قانون مُلزِم للتطبيقين (البنية · Cubit · طبقة الشبكة · الأعلام). - السائق: أُزيل Dart القديم (GetX) مع الإبقاء على الأصيل والإضافات وشهادات التوقيع وShorebird وFirebase الخاص به، وأُعيد هيكلته مطابقاً للراكب. - طبقة الشبكة ترسل x-device-id وتجدّد التوكن عند 401 مرة واحدة فقط. - الحزم موحّدة بين التطبيقين، والـAPI https حصراً في الاثنين. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
3ab74a13a8
commit
de4bbd00ac
@@ -0,0 +1,46 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../data/auth_repository.dart';
|
||||
import 'auth_state.dart';
|
||||
|
||||
class AuthCubit extends Cubit<AuthState> {
|
||||
final AuthRepository _repo;
|
||||
|
||||
AuthCubit(this._repo) : super(const AuthState());
|
||||
|
||||
Future<void> checkAuth() async {
|
||||
final user = await _repo.me();
|
||||
emit(state.copyWith(
|
||||
status: user != null ? AuthStatus.authenticated : AuthStatus.unauthenticated,
|
||||
user: user,
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> sendOtp(String phone) async {
|
||||
if (phone.trim().isEmpty) {
|
||||
emit(state.copyWith(status: AuthStatus.error, error: 'أدخل رقم الهاتف'));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
emit(state.copyWith(status: AuthStatus.authenticating, phone: phone.trim()));
|
||||
await _repo.sendOtp(phone.trim());
|
||||
emit(state.copyWith(status: AuthStatus.codeSent, phone: phone.trim()));
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: AuthStatus.error, error: 'تعذّر إرسال الرمز'));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> verify(String code) async {
|
||||
try {
|
||||
emit(state.copyWith(status: AuthStatus.authenticating));
|
||||
final user = await _repo.verify(state.phone, code.trim());
|
||||
emit(state.copyWith(status: AuthStatus.authenticated, user: user));
|
||||
} catch (_) {
|
||||
emit(state.copyWith(status: AuthStatus.error, error: 'رمز غير صحيح'));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _repo.logout();
|
||||
emit(const AuthState(status: AuthStatus.unauthenticated));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import '../data/auth_repository.dart';
|
||||
|
||||
enum AuthStatus {
|
||||
unknown,
|
||||
unauthenticated,
|
||||
codeSent,
|
||||
authenticating,
|
||||
authenticated,
|
||||
error,
|
||||
}
|
||||
|
||||
class AuthState extends Equatable {
|
||||
final AuthStatus status;
|
||||
final String phone;
|
||||
final AppUser? user;
|
||||
final String? error;
|
||||
|
||||
const AuthState({
|
||||
this.status = AuthStatus.unknown,
|
||||
this.phone = '',
|
||||
this.user,
|
||||
this.error,
|
||||
});
|
||||
|
||||
AuthState copyWith({
|
||||
AuthStatus? status,
|
||||
String? phone,
|
||||
AppUser? user,
|
||||
String? error,
|
||||
}) {
|
||||
return AuthState(
|
||||
status: status ?? this.status,
|
||||
phone: phone ?? this.phone,
|
||||
user: user ?? this.user,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, phone, user, error];
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/storage/token_store.dart';
|
||||
|
||||
class AppUser {
|
||||
final String id;
|
||||
final String phone;
|
||||
final String? name;
|
||||
final String role;
|
||||
|
||||
AppUser({required this.id, required this.phone, this.name, required this.role});
|
||||
|
||||
factory AppUser.fromJson(Map<String, dynamic> j) => AppUser(
|
||||
id: j['id'] as String,
|
||||
phone: (j['phone'] ?? '') as String,
|
||||
name: j['name'] as String?,
|
||||
role: (j['role'] ?? 'driver') as String,
|
||||
);
|
||||
}
|
||||
|
||||
class AuthRepository {
|
||||
final ApiClient _api;
|
||||
final TokenStore _store;
|
||||
|
||||
AuthRepository(this._api, this._store);
|
||||
|
||||
Future<void> sendOtp(String phone) async {
|
||||
await _api.dio.post('/auth/send-otp', data: {'phone': phone});
|
||||
}
|
||||
|
||||
Future<AppUser> verify(String phone, String code) async {
|
||||
final r = await _api.dio.post(
|
||||
'/auth/verify-otp',
|
||||
data: {'phone': phone, 'code': code},
|
||||
);
|
||||
await _store.save(
|
||||
r.data['access_token'] as String,
|
||||
r.data['refresh_token'] as String,
|
||||
);
|
||||
return AppUser.fromJson(Map<String, dynamic>.from(r.data['user'] as Map));
|
||||
}
|
||||
|
||||
Future<AppUser?> me() async {
|
||||
try {
|
||||
final r = await _api.dio.get('/users/me');
|
||||
return AppUser.fromJson(Map<String, dynamic>.from(r.data as Map));
|
||||
} on DioException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() => _store.clear();
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../cubit/auth_cubit.dart';
|
||||
import '../cubit/auth_state.dart';
|
||||
|
||||
class OtpScreen extends StatefulWidget {
|
||||
const OtpScreen({super.key});
|
||||
@override
|
||||
State<OtpScreen> createState() => _OtpScreenState();
|
||||
}
|
||||
|
||||
class _OtpScreenState extends State<OtpScreen> {
|
||||
final _controller = TextEditingController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_forward),
|
||||
onPressed: () => context.go('/phone'),
|
||||
),
|
||||
),
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: BlocConsumer<AuthCubit, AuthState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == AuthStatus.error && state.error != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(state.error!)),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
final loading = state.status == AuthStatus.authenticating;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const SizedBox(height: 24),
|
||||
Text('أدخل رمز التحقق',
|
||||
style: Theme.of(context).textTheme.titleLarge),
|
||||
const SizedBox(height: 8),
|
||||
Text('أُرسل إلى ${state.phone}'),
|
||||
const SizedBox(height: 28),
|
||||
TextField(
|
||||
controller: _controller,
|
||||
keyboardType: TextInputType.number,
|
||||
textAlign: TextAlign.center,
|
||||
decoration: const InputDecoration(labelText: 'الرمز'),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
FilledButton(
|
||||
onPressed: loading
|
||||
? null
|
||||
: () => context.read<AuthCubit>().verify(_controller.text),
|
||||
child: loading
|
||||
? const SizedBox(
|
||||
height: 22, width: 22,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('تأكيد'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../cubit/auth_cubit.dart';
|
||||
import '../cubit/auth_state.dart';
|
||||
|
||||
class PhoneScreen extends StatefulWidget {
|
||||
const PhoneScreen({super.key});
|
||||
@override
|
||||
State<PhoneScreen> createState() => _PhoneScreenState();
|
||||
}
|
||||
|
||||
class _PhoneScreenState extends State<PhoneScreen> {
|
||||
final _controller = TextEditingController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const SizedBox(height: 40),
|
||||
Text('Tripz', style: Theme.of(context).textTheme.headlineMedium),
|
||||
const SizedBox(height: 8),
|
||||
const Text('أدخل رقم هاتفك للمتابعة'),
|
||||
const SizedBox(height: 32),
|
||||
TextField(
|
||||
controller: _controller,
|
||||
keyboardType: TextInputType.phone,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'رقم الهاتف',
|
||||
hintText: '07XXXXXXXX',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
BlocConsumer<AuthCubit, AuthState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == AuthStatus.error && state.error != null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(state.error!)),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
final loading = state.status == AuthStatus.authenticating;
|
||||
return FilledButton(
|
||||
onPressed: loading
|
||||
? null
|
||||
: () => context.read<AuthCubit>().sendOtp(_controller.text),
|
||||
child: loading
|
||||
? const SizedBox(
|
||||
height: 22, width: 22,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('إرسال الرمز'),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user