import 'package:flutter/material.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'auth_service.dart'; import 'login_or_register.dart'; import '../../constants.dart'; import '../../pages/home_page.dart'; import '../../pages/user_data_page.dart'; class AuthGate extends StatelessWidget { const AuthGate({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: StreamBuilder( stream: FirebaseAuth.instance.authStateChanges(), builder: (context, snapshot) { // check if user is logged in or not if (snapshot.hasData) { return FutureBuilder( // check database entries, if data is missing // then complete registration process // else go to HomePage future: _checkCollectionsExist(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const CircularProgressIndicator(); } else if (snapshot.hasData && snapshot.data == true) { return HomePage(); } else { // also in case (snapshot.hasError) return const UserDataPage(isRegProcess: true); } }, ); } else { return const LoginOrRegister(); } }, ), ); } Future _checkCollectionsExist() async { bool languagesExist = await _checkUsersCollectionExists(Constants.dbCollectionLanguages); bool locationsExist = await _checkUsersCollectionExists(Constants.dbCollectionLocations); return languagesExist && locationsExist; } Future _checkUsersCollectionExists(String collectionName) async { String currentUserId = AuthService().getCurrentUser()!.uid; final collection = FirebaseFirestore.instance .collection(Constants.dbCollectionUsers) .doc(currentUserId) .collection(collectionName); final snapshot = await collection.limit(1).get(); return snapshot.docs.isNotEmpty; } }