cofounderella/lib/services/auth/auth_gate.dart

126 lines
4.2 KiB
Dart
Raw Permalink Normal View History

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-06-20 03:55:20 +02:00
2024-05-17 23:08:26 +02:00
import '../../constants.dart';
import '../../pages/home_page.dart';
2024-06-20 03:55:20 +02:00
import '../../pages/welcome_page.dart';
import 'auth_service.dart';
import 'login_or_register.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(
2024-05-20 01:16:54 +02:00
// check if user document exists
future: _checkUserExists(),
2024-05-17 23:08:26 +02:00
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasData && snapshot.data == true) {
2024-05-20 01:16:54 +02:00
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) {
2024-06-09 00:04:13 +02:00
return const HomePage();
2024-05-20 01:16:54 +02:00
} else {
// also in case of (snapshot.hasError)
2024-06-20 03:55:20 +02:00
return const WelcomePage();
2024-05-20 01:16:54 +02:00
}
},
);
2024-05-17 23:08:26 +02:00
} else {
2024-05-20 01:16:54 +02:00
// should user entry not exist, recreate it and force logout
_createUserEntry();
FirebaseAuth.instance.signOut();
return const LoginOrRegister();
2024-05-17 23:08:26 +02:00
}
},
);
} else {
2024-04-29 14:36:25 +02:00
return const LoginOrRegister();
}
},
),
);
}
2024-05-17 23:08:26 +02:00
2024-05-20 01:16:54 +02:00
Future<bool> _checkUserExists() async {
// return true to avoid unneeded read access to database
return true;
/* enable code to check for deleted users
try {
String currentUserId = AuthService().getCurrentUser()!.uid;
final userDoc = await FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(currentUserId)
.get();
return userDoc.exists;
} catch (e) {
print(e.toString());
return false;
}
*/
}
Future<void> _createUserEntry() async {
try {
var currentUser = AuthService().getCurrentUser();
String currentUserId = currentUser!.uid;
String email = currentUser.email!;
await FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(currentUserId)
.set({
Constants.dbFieldUsersID: currentUserId,
Constants.dbFieldUsersEmail: email,
2024-05-20 01:16:54 +02:00
});
} catch (e) {
2024-06-07 02:18:20 +02:00
debugPrint("Error creating user document: $e");
2024-05-20 01:16:54 +02:00
}
}
2024-05-17 23:08:26 +02:00
Future<bool> _checkCollectionsExist() async {
2024-05-20 01:16:54 +02:00
try {
bool languagesExist =
await _checkUsersCollectionExists(Constants.dbCollectionLanguages);
bool locationsExist =
await _checkUsersCollectionExists(Constants.dbCollectionLocations);
return languagesExist && locationsExist;
} catch (e) {
2024-06-07 16:58:29 +02:00
debugPrint('DebugPrint: checkCollectionsExist -> ${e.toString()}');
2024-05-20 01:16:54 +02:00
return false;
}
2024-05-17 23:08:26 +02:00
}
Future<bool> _checkUsersCollectionExists(String collectionName) async {
2024-05-20 01:16:54 +02:00
try {
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;
} catch (e) {
2024-06-07 16:58:29 +02:00
debugPrint('DebugPrint: checkUsersCollectionExists -> ${e.toString()}');
2024-05-20 01:16:54 +02:00
return false;
}
2024-05-17 23:08:26 +02:00
}
}