Initial commit: Uruk Prize Platform architecture, backend core, mobile app, gateway caller & deployment pipeline
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../core/network/api_client.dart';
|
||||
import '../../../core/security/fingerprint_service.dart';
|
||||
import 'auth_state.dart';
|
||||
|
||||
class AuthCubit extends Cubit<AuthState> {
|
||||
final ApiClient _apiClient;
|
||||
final FingerprintService _fingerprintService;
|
||||
|
||||
AuthCubit({
|
||||
ApiClient? apiClient,
|
||||
FingerprintService? fingerprintService,
|
||||
}) : _apiClient = apiClient ?? ApiClient(),
|
||||
_fingerprintService = fingerprintService ?? FingerprintService(),
|
||||
super(AuthInitial());
|
||||
|
||||
Future<void> checkExistingSession() async {
|
||||
try {
|
||||
final token = await _fingerprintService.getSessionToken();
|
||||
final userId = await _fingerprintService.getUserId();
|
||||
final fingerprint = await _fingerprintService.getDeviceFingerprint();
|
||||
|
||||
if (token != null && userId != null) {
|
||||
emit(Authenticated(
|
||||
user: {'id': userId, 'full_name': 'عضو أوروك'},
|
||||
token: token,
|
||||
deviceFingerprint: fingerprint,
|
||||
));
|
||||
} else {
|
||||
emit(Unauthenticated());
|
||||
}
|
||||
} catch (_) {
|
||||
emit(Unauthenticated());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> requestOtp(String phone) async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final response = await _apiClient.post(
|
||||
'/api/v1/auth/request-otp',
|
||||
body: {'phone': phone},
|
||||
requiresAuth: false,
|
||||
);
|
||||
|
||||
final data = response['data'] as Map<String, dynamic>?;
|
||||
emit(AuthOtpSent(
|
||||
phone: phone,
|
||||
mockOtp: data?['mock_otp'] as String?,
|
||||
));
|
||||
} catch (e) {
|
||||
emit(AuthFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> verifyOtpAndBindDevice({
|
||||
required String phone,
|
||||
required String otp,
|
||||
String? fullName,
|
||||
}) async {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
final fingerprint = await _fingerprintService.getDeviceFingerprint();
|
||||
final model = await _fingerprintService.getDeviceModel();
|
||||
|
||||
final response = await _apiClient.post(
|
||||
'/api/v1/auth/verify-otp',
|
||||
body: {
|
||||
'phone': phone,
|
||||
'otp': otp,
|
||||
'device_fingerprint': fingerprint,
|
||||
'device_model': model,
|
||||
if (fullName != null && fullName.isNotEmpty) 'full_name': fullName,
|
||||
},
|
||||
requiresAuth: false,
|
||||
);
|
||||
|
||||
final data = response['data'] as Map<String, dynamic>;
|
||||
final user = data['user'] as Map<String, dynamic>;
|
||||
final token = data['token'] as String;
|
||||
final secret = data['device_secret'] as String;
|
||||
final userId = user['id'] as int;
|
||||
|
||||
await _fingerprintService.saveAuthSession(
|
||||
userId: userId,
|
||||
token: token,
|
||||
deviceSecret: secret,
|
||||
);
|
||||
|
||||
emit(Authenticated(
|
||||
user: user,
|
||||
token: token,
|
||||
deviceFingerprint: fingerprint,
|
||||
));
|
||||
} catch (e) {
|
||||
emit(AuthFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _fingerprintService.clearSession();
|
||||
emit(Unauthenticated());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
abstract class AuthState extends Equatable {
|
||||
const AuthState();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AuthInitial extends AuthState {}
|
||||
|
||||
class AuthLoading extends AuthState {}
|
||||
|
||||
class AuthOtpSent extends AuthState {
|
||||
final String phone;
|
||||
final String? mockOtp;
|
||||
|
||||
const AuthOtpSent({required this.phone, this.mockOtp});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [phone, mockOtp];
|
||||
}
|
||||
|
||||
class Authenticated extends AuthState {
|
||||
final Map<String, dynamic> user;
|
||||
final String token;
|
||||
final String deviceFingerprint;
|
||||
|
||||
const Authenticated({
|
||||
required this.user,
|
||||
required this.token,
|
||||
required this.deviceFingerprint,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [user, token, deviceFingerprint];
|
||||
}
|
||||
|
||||
class Unauthenticated extends AuthState {}
|
||||
|
||||
class AuthFailure extends AuthState {
|
||||
final String errorMessage;
|
||||
|
||||
const AuthFailure(this.errorMessage);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [errorMessage];
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../core/theme/app_theme.dart';
|
||||
import '../cubit/auth_cubit.dart';
|
||||
import '../cubit/auth_state.dart';
|
||||
|
||||
class PhoneAuthScreen extends StatefulWidget {
|
||||
const PhoneAuthScreen({super.key});
|
||||
|
||||
@override
|
||||
State<PhoneAuthScreen> createState() => _PhoneAuthScreenState();
|
||||
}
|
||||
|
||||
class _PhoneAuthScreenState extends State<PhoneAuthScreen> {
|
||||
final _phoneController = TextEditingController();
|
||||
final _otpController = TextEditingController();
|
||||
final _nameController = TextEditingController();
|
||||
bool _isOtpStage = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_phoneController.dispose();
|
||||
_otpController.dispose();
|
||||
_nameController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Directionality(
|
||||
textDirection: TextDirection.rtl,
|
||||
child: Scaffold(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [Color(0xFF0B1120), Color(0xFF020617)],
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 28),
|
||||
child: BlocConsumer<AuthCubit, AuthState>(
|
||||
listener: (context, state) {
|
||||
if (state is AuthOtpSent) {
|
||||
setState(() {
|
||||
_isOtpStage = true;
|
||||
if (state.mockOtp != null) {
|
||||
_otpController.text = state.mockOtp!;
|
||||
}
|
||||
});
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('تم إرسال رمز التحقق عبر واتساب / الرسائل القصيرة.'),
|
||||
backgroundColor: AppTheme.emeraldGreen,
|
||||
),
|
||||
);
|
||||
} else if (state is AuthFailure) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(state.errorMessage),
|
||||
backgroundColor: AppTheme.crimsonRed,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
builder: (context, state) {
|
||||
final isLoading = state is AuthLoading;
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
// Uruk Prize Emblem / Emblem Placeholder
|
||||
Container(
|
||||
width: 90,
|
||||
height: 90,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: AppTheme.goldAccent, width: 2),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: AppTheme.goldAccent.withOpacity(0.25),
|
||||
blurRadius: 20,
|
||||
spreadRadius: 3,
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Center(
|
||||
child: Icon(
|
||||
Icons.workspace_premium,
|
||||
color: AppTheme.goldAccent,
|
||||
size: 46,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
|
||||
const Text(
|
||||
'جائزة أوروك الدولية',
|
||||
style: TextStyle(
|
||||
color: AppTheme.goldLight,
|
||||
fontSize: 26,
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 0.5,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
const Text(
|
||||
'الهوية الرقمية الموحدة للمزايا الصحية والتدريبية',
|
||||
style: TextStyle(
|
||||
color: AppTheme.textMuted,
|
||||
fontSize: 14,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 36),
|
||||
|
||||
if (!_isOtpStage) ...[
|
||||
// Full Name
|
||||
TextField(
|
||||
controller: _nameController,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'الاسم الكامل',
|
||||
labelStyle: const TextStyle(color: AppTheme.textMuted),
|
||||
prefixIcon: const Icon(Icons.person, color: AppTheme.goldAccent),
|
||||
filled: true,
|
||||
fillColor: AppTheme.cardDark,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Phone Number
|
||||
TextField(
|
||||
controller: _phoneController,
|
||||
keyboardType: TextInputType.phone,
|
||||
textDirection: TextDirection.ltr,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'رقم الهاتف (مع الرمز الدولي)',
|
||||
labelStyle: const TextStyle(color: AppTheme.textMuted),
|
||||
hintText: '+964 770 000 0000',
|
||||
hintStyle: const TextStyle(color: Colors.white24),
|
||||
prefixIcon: const Icon(Icons.phone_android, color: AppTheme.goldAccent),
|
||||
filled: true,
|
||||
fillColor: AppTheme.cardDark,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 52,
|
||||
child: ElevatedButton(
|
||||
onPressed: isLoading
|
||||
? null
|
||||
: () {
|
||||
final phone = _phoneController.text.trim();
|
||||
if (phone.isNotEmpty) {
|
||||
context.read<AuthCubit>().requestOtp(phone);
|
||||
}
|
||||
},
|
||||
child: isLoading
|
||||
? const CircularProgressIndicator(color: AppTheme.primaryDark)
|
||||
: const Text('إرسال رمز التحقق'),
|
||||
),
|
||||
),
|
||||
] else ...[
|
||||
// OTP Input
|
||||
TextField(
|
||||
controller: _otpController,
|
||||
keyboardType: TextInputType.number,
|
||||
textAlign: TextAlign.center,
|
||||
maxLength: 6,
|
||||
style: const TextStyle(
|
||||
color: AppTheme.goldLight,
|
||||
fontSize: 24,
|
||||
letterSpacing: 10,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'رمز التحقق السري (OTP)',
|
||||
labelStyle: const TextStyle(color: AppTheme.textMuted),
|
||||
counterText: '',
|
||||
filled: true,
|
||||
fillColor: AppTheme.cardDark,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'تم الإرسال إلى ${_phoneController.text}',
|
||||
style: const TextStyle(color: AppTheme.textMuted, fontSize: 13),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 52,
|
||||
child: ElevatedButton(
|
||||
onPressed: isLoading
|
||||
? null
|
||||
: () {
|
||||
final otp = _otpController.text.trim();
|
||||
if (otp.length == 6) {
|
||||
context.read<AuthCubit>().verifyOtpAndBindDevice(
|
||||
phone: _phoneController.text.trim(),
|
||||
otp: otp,
|
||||
fullName: _nameController.text.trim(),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: isLoading
|
||||
? const CircularProgressIndicator(color: AppTheme.primaryDark)
|
||||
: const Text('تأكيد الدخول وربط الجهاز'),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
TextButton(
|
||||
onPressed: () => setState(() => _isOtpStage = false),
|
||||
child: const Text(
|
||||
'تعديل رقم الهاتف',
|
||||
style: TextStyle(color: AppTheme.goldAccent),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user