cofounderella/lib/services/auth/auth_gate.dart

128 lines
4.3 KiB
Dart

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 if user document exists
future: _checkUserExists(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasData && snapshot.data == true) {
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 of (snapshot.hasError)
return const UserDataPage(
isRegProcess: true,
isEditMode: false,
);
}
},
);
} else {
// should user entry not exist, recreate it and force logout
_createUserEntry();
FirebaseAuth.instance.signOut();
return const LoginOrRegister();
}
},
);
} else {
return const LoginOrRegister();
}
},
),
);
}
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,
});
} catch (e) {
debugPrint("Error creating user document: $e");
}
}
Future<bool> _checkCollectionsExist() async {
try {
bool languagesExist =
await _checkUsersCollectionExists(Constants.dbCollectionLanguages);
bool locationsExist =
await _checkUsersCollectionExists(Constants.dbCollectionLocations);
return languagesExist && locationsExist;
} catch (e) {
debugPrint('DebugPrint: checkCollectionsExist -> ${e.toString()}');
return false;
}
}
Future<bool> _checkUsersCollectionExists(String collectionName) async {
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) {
debugPrint('DebugPrint: checkUsersCollectionExists -> ${e.toString()}');
return false;
}
}
}