AuthServiec.dart 1.7 KB

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