AuthService.dart 1.6 KB

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