import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:physigo/Services/DatabaseManager.dart'; import 'package:physigo/Services/logged_in_user.dart'; class AuthenticationServices { static final FirebaseAuth _auth = FirebaseAuth.instance; static LoggedInUser? user; //Register a user static Future createNewUser(String address, bool anonymous, String birth, String name, String email, String password, String phone, String sharedID, String surname) async { try { UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password); User? user = result.user; await DatabaseManager("total_points").createUserData(address, anonymous, birth, DateTime.now(), DateTime.now(), email, name, phone, sharedID, surname, 0, DateTime.now(), 'null', user!.uid); return user; } catch (e) { print(e.toString()); } } //LogIn with user static Future loginUser(String email, String password) async { try { UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password); await getCurrentUser(); return result.user; } catch (e) { print(e.toString()); } } static Future getCurrentUser() async { if (user != null) { return user; } if (_auth.currentUser == null) { return null; } var querySnapshot = await FirebaseFirestore.instance .collection('profileInfo') .where('mail', isEqualTo: _auth.currentUser?.email) .get(); user = LoggedInUser.fromMap(querySnapshot.docs.first.data()); return user; } static Stream getPlace(dynamic user) async* { var users = await FirebaseFirestore.instance .collection('Users') .where("total_points", isGreaterThan: user["total_points"]) .get(); yield users.docs.length + 1; } }