Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ package com.ref.rrs.feature.authentication.data

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import com.google.firebase.Firebase
import com.google.firebase.auth.FirebaseAuth
import androidx.core.content.edit
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.firestore
import com.google.firebase.firestore.FirebaseFirestore
import com.ref.rrs.feature.authentication.domain.Resource
import com.ref.rrs.feature.authentication.domain.User
import com.ref.rrs.feature.authentication.domain.UserRole
import kotlinx.coroutines.tasks.await
import javax.inject.Inject
import javax.inject.Singleton
import androidx.core.content.edit
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Source

@Singleton
class AuthRepositoryImpl(
class AuthRepositoryImpl @Inject constructor(
private val firestore: FirebaseFirestore,
private val app: Application
) : AuthRepository {

private val usersCollection = firestore.collection("users")

override suspend fun login(
identifier: String,
password: String
Expand All @@ -34,28 +31,36 @@ class AuthRepositoryImpl(
} else {
getUserEmailByPhone(identifier) ?: return Resource.Error("User not found")
}
val userIds = firestore.collection("users")
val querySnapshot = usersCollection
.whereEqualTo("email", userEmail)
.limit(1)
.get()
.await()
if(userIds.isEmpty) {
Resource.Error("Login failed")
} else {
app.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.edit {
putString("currentUserId", userIds.documents[0].id)
}
Resource.Success(Unit)
if (querySnapshot.isEmpty) {
return Resource.Error("User not found")
}
val document = querySnapshot.documents[0]
val storedPassword = document.getString("password")
if (storedPassword != password) {
return Resource.Error("Incorrect password")
}
val isActive = document.getBoolean("isActive") ?: false
if (!isActive) {
return Resource.Error("Account is deactivated")
}
app.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.edit {
putString("currentUserId", document.id)
}
Resource.Success(Unit)
} catch (e: Exception) {
Resource.Error(e.message ?: "Login failed")
}
}

private suspend fun getUserEmailByPhone(phone: String): String? {
return try {
val snapshot = Firebase.firestore.collection("users")
val snapshot = usersCollection
.whereEqualTo("phone", phone)
.limit(1)
.get()
Expand All @@ -79,55 +84,44 @@ class AuthRepositoryImpl(
}

override suspend fun getCurrentUser(): User? {
app.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.getString("currentUserId", "")?.let { id ->
if(id.isNotBlank()) {
return firestore.collection("users")
.document(id)
.get()
.await()
.toUser()
}
}
return null
}

private suspend fun getUserById(userId: String): User? {
val id = app.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.getString("currentUserId", null)
if (id.isNullOrBlank()) return null
return try {
val document = Firebase.firestore.collection("users")
.document(userId)
val document = usersCollection
.document(id)
.get()
.await()

if (document.exists()) {
document.toUser()
} else {
null
}
} catch (e: Exception) {
} catch (_: Exception) {
null
}
}

private fun DocumentSnapshot.toUser(): User {
return User(
fullName = getString("fullName")!!,
role = UserRole.valueOf(getString("role")!!),
address = getString("address")!!,
phone = getString("phone")!!,
phoneAlt = getString("phoneAlt")!!,
email = getString("email")!!,
password = getString("password")!!,
isActive = getBoolean("isActive")!!
fullName = getString("fullName") ?: "",
role = try {
UserRole.valueOf(getString("role") ?: "EXECUTOR")
} catch (_: IllegalArgumentException) {
UserRole.EXECUTOR
},
address = getString("address") ?: "",
phone = getString("phone") ?: "",
phoneAlt = getString("phoneAlt") ?: "",
email = getString("email") ?: "",
password = getString("password") ?: "",
isActive = getBoolean("isActive") ?: false
)
}

override suspend fun isLoggedIn(): Boolean {
return app.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.getString("currentUserId", "")?.let { id ->
if(id.isBlank())
false
true
} ?: false
val id = app.getSharedPreferences("prefs", Context.MODE_PRIVATE)
.getString("currentUserId", null)
return !id.isNullOrBlank()
}
}
}