@@ -3,31 +3,53 @@ package fr.utbm.sy43.pilulito.data.repositories
33import com.google.firebase.firestore.FirebaseFirestore
44import fr.utbm.sy43.pilulito.data.models.AccountType
55import fr.utbm.sy43.pilulito.data.models.User
6+ import kotlinx.coroutines.CoroutineScope
7+ import kotlinx.coroutines.Dispatchers
68import kotlinx.coroutines.flow.MutableStateFlow
79import kotlinx.coroutines.flow.StateFlow
810import kotlinx.coroutines.flow.asStateFlow
11+ import kotlinx.coroutines.launch
912import kotlinx.coroutines.tasks.await
1013import java.util.UUID
1114
1215class FirestoreAuthRepository (
13- private val firestore : FirebaseFirestore
16+ private val firestore : FirebaseFirestore ,
17+ private val firebaseAuth : com.google.firebase.auth.FirebaseAuth
1418) : AuthRepository {
1519
1620 private val _currentUser = MutableStateFlow <User ?>(null )
1721 override val currentUser: StateFlow <User ?> = _currentUser .asStateFlow()
18-
1922 private val usersCollection = firestore.collection(" users" )
2023
24+ // check if user was logged on
25+ override suspend fun checkExistingSession (): User ? {
26+ val firebaseUser = firebaseAuth.currentUser ? : return null
27+ try {
28+ val snapshot = usersCollection.document(firebaseUser.uid).get().await()
29+ val user = snapshot.toObject(User ::class .java)
30+ _currentUser .value = user
31+ return user
32+ } catch (e: Exception ) {
33+ e.printStackTrace()
34+ return null
35+ }
36+ }
37+
38+
39+
40+
2141 override suspend fun login (email : String , password : String ): User ? {
2242 try {
23- // look for the right user
24- val snapshot = usersCollection
25- .whereEqualTo(" email" , email)
26- .whereEqualTo(" password" , password)
27- .get()
28- .await()
43+ // check email and password
44+ val authResult = firebaseAuth.signInWithEmailAndPassword(email, password).await()
45+
46+
47+ val firebaseUserId = authResult.user?.uid ? : return null
48+
49+ // if email and password correct, then get all the other information from firestore
50+ val snapshot = usersCollection.document(firebaseUserId).get().await()
51+ val user = snapshot.toObject(User ::class .java)
2952
30- val user = snapshot.documents.firstOrNull()?.toObject(User ::class .java)
3153 _currentUser .value = user
3254 return user
3355 } catch (e: Exception ) {
@@ -45,41 +67,41 @@ class FirestoreAuthRepository(
4567 supervisorLinkCode : String?
4668 ): User ? {
4769 try {
48- // email must be unique
49- val emailCheck = usersCollection.whereEqualTo(" email" , email).get().await()
50- if (! emailCheck.isEmpty) return null
51-
5270 var linkedSeniorId: String? = null
5371
54- // if supervisor then look for the senior's code
72+ // if supervisor, look for the senior's code
5573 if (accountType == AccountType .SUPERVISOR ) {
5674 if (supervisorLinkCode.isNullOrBlank()) return null
5775 val senior = findSeniorByLinkCode(supervisorLinkCode) ? : return null
5876 linkedSeniorId = senior.id
5977 }
6078
61- val userId = UUID .randomUUID().toString()
79+ // account creation
80+ val authResult = firebaseAuth.createUserWithEmailAndPassword(email, password).await()
81+
82+ // get id from firebase auth
83+ val firebaseUserId = authResult.user?.uid ? : return null
84+
85+ // set data for firestore
6286 val newUser = User (
63- id = userId,
87+ id = firebaseUserId, // id from firebase auth
6488 firstName = name,
6589 lastName = surname,
6690 email = email,
67- password = password,
6891 accountType = accountType,
6992 linkCode = generateLinkCode(name),
7093 linkedSeniorId = linkedSeniorId
7194 )
7295
73- // save in firestore
74- usersCollection.document(userId).set(newUser).await()
96+ usersCollection.document(firebaseUserId).set(newUser).await()
97+ // update local session
7598 _currentUser .value = newUser
7699 return newUser
77100 } catch (e: Exception ) {
78101 e.printStackTrace()
79102 return null
80103 }
81104 }
82-
83105 override suspend fun findSeniorByLinkCode (linkCode : String ): User ? {
84106 try {
85107 val snapshot = usersCollection
@@ -96,13 +118,21 @@ class FirestoreAuthRepository(
96118 }
97119
98120 override suspend fun logout () {
121+ firebaseAuth.signOut()
99122 _currentUser .value = null
100123 }
101124
102125 override suspend fun updateEmail (newEmail : String ) {
103126 try {
104127 val userId = _currentUser .value?.id ? : return
128+
129+ // update in fireAuth
130+ // updateEmail IS DEPRECATED AND NOT WORKING
131+ firebaseAuth.currentUser?.updateEmail(newEmail)?.await()
132+
133+ // update in firestore
105134 usersCollection.document(userId).update(" email" , newEmail).await()
135+
106136 _currentUser .value = _currentUser .value?.copy(email = newEmail)
107137 } catch (e: Exception ) {
108138 e.printStackTrace()
@@ -111,9 +141,8 @@ class FirestoreAuthRepository(
111141
112142 override suspend fun updatePassword (newPassword : String ) {
113143 try {
114- val userId = _currentUser .value?.id ? : return
115- usersCollection.document(userId).update(" password" , newPassword).await()
116- _currentUser .value = _currentUser .value?.copy(password = newPassword)
144+ // update in fireauth
145+ firebaseAuth.currentUser?.updatePassword(newPassword)?.await()
117146 } catch (e: Exception ) {
118147 e.printStackTrace()
119148 }
@@ -123,6 +152,7 @@ class FirestoreAuthRepository(
123152 try {
124153 val userId = _currentUser .value?.id ? : return
125154 usersCollection.document(userId).delete().await()
155+ firebaseAuth.currentUser?.delete()?.await()
126156 _currentUser .value = null
127157 } catch (e: Exception ) {
128158 e.printStackTrace()
0 commit comments