AuthService.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import 'package:cloud_firestore/cloud_firestore.dart';
  2. import 'package:firebase_auth/firebase_auth.dart';
  3. import 'package:physigo/Services/DatabaseManager.dart';
  4. import 'package:physigo/Services/logged_in_user.dart';
  5. class AuthenticationServices {
  6. static final FirebaseAuth _auth = FirebaseAuth.instance;
  7. static LoggedInUser? user;
  8. //Register a user
  9. static Future createNewUser(String address, bool anonymous, String birth, String name, String email, String password,
  10. String phone, String sharedID, String surname) async {
  11. try {
  12. UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
  13. User? user = result.user;
  14. await DatabaseManager("total_points").createUserData(address, anonymous, birth, DateTime.now(), DateTime.now(),
  15. email, name, phone, sharedID, surname, 0, DateTime.now(), 'null', user!.uid);
  16. return user;
  17. } catch (e) {
  18. print(e.toString());
  19. }
  20. }
  21. //LogIn with user
  22. static Future loginUser(String email, String password) async {
  23. try {
  24. UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
  25. await getCurrentUser();
  26. return result.user;
  27. } catch (e) {
  28. print(e.toString());
  29. }
  30. }
  31. static Future<LoggedInUser?> getCurrentUser() async {
  32. if (user != null) {
  33. return user;
  34. }
  35. if (_auth.currentUser == null) {
  36. return null;
  37. }
  38. var querySnapshot = await FirebaseFirestore.instance
  39. .collection('profileInfo')
  40. .where('mail', isEqualTo: _auth.currentUser?.email)
  41. .get();
  42. user = LoggedInUser.fromMap(querySnapshot.docs.first.data());
  43. return user;
  44. }
  45. static Stream<int> getPlace(dynamic user) async* {
  46. var users = await FirebaseFirestore.instance
  47. .collection('Users')
  48. .where("total_points", isGreaterThan: user["total_points"])
  49. .get();
  50. yield users.docs.length + 1;
  51. }
  52. }