Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.

Commit 91b7b57

Browse files
Merge branch 'dev' into unit-tests
2 parents 8991774 + 80a7402 commit 91b7b57

8 files changed

Lines changed: 166 additions & 53 deletions

File tree

app/src/main/java/fr/utbm/sy43/pilulito/data/AppContainer.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import fr.utbm.sy43.pilulito.data.repositories.OfflineMedicationRepository
99
import fr.utbm.sy43.pilulito.data.repositories.PharmacyRepository
1010
import com.google.firebase.auth.FirebaseAuth
1111
import com.google.firebase.firestore.FirebaseFirestore
12+
import fr.utbm.sy43.pilulito.data.models.AccountType
1213
import fr.utbm.sy43.pilulito.data.repositories.FirestoreAuthRepository
1314
import fr.utbm.sy43.pilulito.data.repositories.FirestoreMedicationRepository
1415

@@ -38,10 +39,22 @@ class DefaultAppContainer(private val context: Context) : AppContainer {
3839
}
3940

4041
override val authRepository: AuthRepository by lazy {
41-
FirestoreAuthRepository(firestore)
42+
FirestoreAuthRepository(firestore, firebaseAuth)
4243
}
4344

4445
override val medicationRepository: MedicationRepository by lazy {
45-
FirestoreMedicationRepository(firestore)
46+
FirestoreMedicationRepository(firestore, getUserId = {
47+
val appUser = authRepository.currentUser.value
48+
if (appUser != null) {
49+
if (appUser.accountType == AccountType.SUPERVISOR) {
50+
appUser.linkedSeniorId.orEmpty()
51+
} else {
52+
appUser.id
53+
}
54+
} else {
55+
//no connection
56+
""
57+
}
58+
})
4659
}
4760
}

app/src/main/java/fr/utbm/sy43/pilulito/data/models/User.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data class User(
1010
val firstName: String = "",
1111
val lastName: String = "",
1212
val email: String = "",
13-
val password: String = "",
13+
//val password: String = "",
1414
val accountType: AccountType = AccountType.SENIOR,
1515
val linkCode: String = "",
1616
val linkedSeniorId: String? = null

app/src/main/java/fr/utbm/sy43/pilulito/data/repositories/AuthRepository.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface AuthRepository {
2525
suspend fun updatePassword(newPassword: String)
2626
suspend fun deleteAccount()
2727

28+
suspend fun checkExistingSession(): User?
29+
2830
}
2931

3032
class OfflineAuthRepository : AuthRepository {
@@ -38,7 +40,7 @@ class OfflineAuthRepository : AuthRepository {
3840
firstName = "Waltuh",
3941
lastName = "HelpMe",
4042
email = "im.a@labubu.waltuh",
41-
password = "feet",
43+
//password = "feet",
4244
accountType = AccountType.SENIOR,
4345
linkCode = "WALT-1234"
4446
),
@@ -48,7 +50,7 @@ class OfflineAuthRepository : AuthRepository {
4850
firstName = "Jesse",
4951
lastName = "Pinkman",
5052
email = "jesse@supervisor.com",
51-
password = "yo",
53+
//password = "yo",
5254
accountType = AccountType.SUPERVISOR,
5355
linkCode = "JESSE-5678",
5456
linkedSeniorId = "user_1"
@@ -60,11 +62,15 @@ class OfflineAuthRepository : AuthRepository {
6062
users.removeAll { it.id == user.id }
6163
_currentUser.value = null
6264
}
63-
65+
//now password is sent in fireAuth, error with this old function
6466
override suspend fun login(email: String, password: String): User? {
67+
return null
68+
/*
6569
val user = users.find { it.email == email && it.password == password }
6670
_currentUser.value = user
6771
return user
72+
*/
73+
6874
}
6975

7076
override suspend fun signup(
@@ -92,7 +98,7 @@ class OfflineAuthRepository : AuthRepository {
9298
firstName = name,
9399
lastName = surname,
94100
email = email,
95-
password = password,
101+
//password = password,
96102
accountType = accountType,
97103
linkCode = generateLinkCode(name),
98104
linkedSeniorId = linkedSeniorId
@@ -109,10 +115,14 @@ class OfflineAuthRepository : AuthRepository {
109115
}
110116

111117
override suspend fun updatePassword(newPassword: String) {
118+
return
119+
/*
112120
val user = _currentUser.value ?: return
113121
val updated = user.copy(password = newPassword)
114122
users.replaceAll { if (it.id == user.id) updated else it }
115123
_currentUser.value = updated
124+
*/
125+
116126
}
117127

118128
override suspend fun findSeniorByLinkCode(linkCode: String): User? {
@@ -130,4 +140,9 @@ class OfflineAuthRepository : AuthRepository {
130140
val suffix = (1000..9999).random()
131141
return "$prefix-$suffix"
132142
}
143+
144+
//just to implement interface
145+
override suspend fun checkExistingSession(): User?{
146+
return null
147+
}
133148
}

app/src/main/java/fr/utbm/sy43/pilulito/data/repositories/FirestoreAuthRepository.kt

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,53 @@ package fr.utbm.sy43.pilulito.data.repositories
33
import com.google.firebase.firestore.FirebaseFirestore
44
import fr.utbm.sy43.pilulito.data.models.AccountType
55
import fr.utbm.sy43.pilulito.data.models.User
6+
import kotlinx.coroutines.CoroutineScope
7+
import kotlinx.coroutines.Dispatchers
68
import kotlinx.coroutines.flow.MutableStateFlow
79
import kotlinx.coroutines.flow.StateFlow
810
import kotlinx.coroutines.flow.asStateFlow
11+
import kotlinx.coroutines.launch
912
import kotlinx.coroutines.tasks.await
1013
import java.util.UUID
1114

1215
class 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

Comments
 (0)