перенаправлено на вход в систему < /li>
< /ul>
main.dart: написал />>>
Код: Выделить всё
void main() async {
WidgetsFlutterBinding.ensureInitialized();
try {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await SecurityService.initialize();
AntiDebugService.startMonitoring();
ObfuscationUtils.executeDummyOperations();
} catch (error) {
debugPrint('Initialization error: $error');
}
runApp(const KhedamApp());
_initializeBackgroundServices();
}
void _initializeBackgroundServices() async {
try {
FlutterError.onError = (FlutterErrorDetails details) {
RuntimeReportService.reportCrash(
details.exception, details.stack ?? StackTrace.current);
};
runZonedGuarded(() {}, (error, stackTrace) {
RuntimeReportService.reportCrash(error, stackTrace);
});
await RuntimeReportService.init();
AppLifecycleManager().initialize();
final notificationService = NotificationService();
await notificationService.initPushNotifications();
SanctionCleanupService.startPeriodicCleanup();
} catch (error) {
debugPrint('Background initialization error: $error');
}
}
class KhedamApp extends StatefulWidget {
const KhedamApp({super.key});
@override
State createState() => _KhedamAppState();
}
class _KhedamAppState extends State with WidgetsBindingObserver {
final LocaleProvider _localeProvider = LocaleProvider();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed) {
DeviceSessionService.updateActivity();
}
}
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider(create: (_) => AuthService()),
ChangeNotifierProvider.value(value: _localeProvider),
],
child: Consumer(
builder: (context, localeProvider, _) => MaterialApp(
title: 'Khedam',
routes: {'/login': (context) => const LoginScreen()},
debugShowCheckedModeBanner: false,
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('fr'),
Locale('ar'),
Locale('en'),
],
locale: localeProvider.locale,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: AppColors.primary,
brightness: Brightness.light,
),
textTheme: GoogleFonts.notoSansTextTheme(),
useMaterial3: true,
appBarTheme: const AppBarTheme(
backgroundColor: AppColors.primary,
foregroundColor: Colors.white,
elevation: 0,
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.primary,
foregroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
cardTheme: CardTheme(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
home: const SplashScreen(),
),
));
}
}
Код: Выделить всё
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
@override
State createState() => _SplashScreenState();
}
class _SplashScreenState extends State {
bool _hasNavigated = false;
@override
void initState() {
super.initState();
_initializeApp();
Future.delayed(const Duration(seconds: 10), () {
if (!_hasNavigated && mounted) {
if (FirebaseAuth.instance.currentUser != null) {
RuntimeReportService.reportSplashTimeout();
}
}
});
}
void _initializeApp() async {
final splashStart = DateTime.now();
const minSplashDuration = Duration(milliseconds: 2000);
try {
_initializeSplashServices();
final user = FirebaseAuth.instance.currentUser;
final elapsed = DateTime.now().difference(splashStart);
if (elapsed < minSplashDuration) {
await Future.delayed(minSplashDuration - elapsed);
}
if (mounted && !_hasNavigated) {
_hasNavigated = true;
_navigateBasedOnAuthState(user);
}
} catch (e) {
final elapsed = DateTime.now().difference(splashStart);
if (elapsed < minSplashDuration) {
await Future.delayed(minSplashDuration - elapsed);
}
if (mounted && !_hasNavigated) {
_hasNavigated = true;
_navigateBasedOnAuthState(null);
}
}
}
void _initializeSplashServices() async {
try {
SecurityService.performRuntimeCheck();
AntiDebugService.checkPoint();
_startSessionHeartbeat();
} catch (e) {
debugPrint('Splash initialization error: $e');
}
}
void _startSessionHeartbeat() {
Future.delayed(const Duration(minutes: 5), () {
DeviceSessionService.updateActivity();
if (mounted) _startSessionHeartbeat();
});
}
void _navigateBasedOnAuthState(User? user) async {
if (user != null) {
debugPrint('✅ User IS authenticated redirect to home');
DeviceSessionService.updateActivity();
if (mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const HomeScreen()),
);
}
} else {
debugPrint('NO authenticated user redirect to Login');
if (mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const LoginScreen()),
);
}
}
}
Код: Выделить всё
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
User? get currentUser => _auth.currentUser;
Stream get authStateChanges => _auth.authStateChanges();
}
Future signInWithEmailAndPassword(String email, String password) async {
UserCredential result = await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
if (result.user != null) {
return userModel; // Return user data
}
return null;
}
Future signOut() async {
await DeviceSessionService.endSession();
await _googleSignIn.signOut();
await _auth.signOut();
}
< /code>
Я попытался использовать Streambuilder с AuthstateChanges (), но неудачный
Я попытался использовать AuthstateChanges (). Сначала на экране Splash, но не удалось
Оба метода терпит неудачу - состояние авторизации возвращает NULL при запуске холодного приложения, даже если пользователь был ранее аутентифицирован, логики говорят: < /p>
Connection state: ConnectionState.active
Has data: false
User: NULL (NULL)
Подробнее здесь: https://stackoverflow.com/questions/797 ... -gets-logg