2024-04-29 14:36:25 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-17 23:08:26 +02:00
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2024-04-29 14:36:25 +02:00
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
2024-05-17 23:08:26 +02:00
|
|
|
import 'auth_service.dart';
|
|
|
|
import 'login_or_register.dart';
|
|
|
|
import '../../constants.dart';
|
|
|
|
import '../../pages/home_page.dart';
|
|
|
|
import '../../pages/user_data_page.dart';
|
2024-04-29 14:36:25 +02:00
|
|
|
|
2024-05-17 23:08:26 +02:00
|
|
|
class AuthGate extends StatelessWidget {
|
2024-04-29 14:36:25 +02:00
|
|
|
const AuthGate({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
body: StreamBuilder(
|
|
|
|
stream: FirebaseAuth.instance.authStateChanges(),
|
2024-05-17 23:08:26 +02:00
|
|
|
builder: (context, snapshot) {
|
2024-04-29 14:36:25 +02:00
|
|
|
// check if user is logged in or not
|
2024-05-17 23:08:26 +02:00
|
|
|
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 {
|
2024-04-29 14:36:25 +02:00
|
|
|
return const LoginOrRegister();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-17 23:08:26 +02:00
|
|
|
|
|
|
|
Future<bool> _checkCollectionsExist() async {
|
|
|
|
bool languagesExist =
|
|
|
|
await _checkUsersCollectionExists(Constants.dbCollectionLanguages);
|
|
|
|
bool locationsExist =
|
|
|
|
await _checkUsersCollectionExists(Constants.dbCollectionLocations);
|
|
|
|
return languagesExist && locationsExist;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> _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;
|
|
|
|
}
|
|
|
|
}
|