| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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';
- import 'package:location/location.dart';
- class AuthenticationServices {
- static final FirebaseAuth _auth = FirebaseAuth.instance;
- static LoggedInUser? user;
- //Register a user
- static Future createNewUser(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;
- LocationData userLocation = await Location.instance.getLocation();
- GeoPoint userGeoPoint = GeoPoint(userLocation.latitude!, userLocation.longitude!);
- await DatabaseManager("total_points").createUserData(userGeoPoint, anonymous, birth, DateTime.now(), DateTime.utc(1970, 1, 1),
- 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<LoggedInUser?> 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 Future<int> getPlace() async {
- var users = await FirebaseFirestore.instance
- .collection('Users')
- .where("total_points", isGreaterThan: user!.totalPoints)
- .get();
- return users.docs.length + 1;
- }
- }
|