test
This commit is contained in:
@@ -41,10 +41,11 @@ import androidx.security.crypto.MasterKey
|
||||
import com.example.scanwich.LoginScreen
|
||||
import com.example.scanwich.MainApp
|
||||
import com.example.scanwich.AccessDeniedScreen
|
||||
import com.google.firebase.firestore.FirebaseFirestore
|
||||
|
||||
@Composable
|
||||
@Suppress("DEPRECATION")
|
||||
fun AuthWrapper(dao: AppDao, ) {
|
||||
fun AuthWrapper(dao: AppDao) {
|
||||
val context = LocalContext.current
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val auth = remember { FirebaseAuth.getInstance() }
|
||||
@@ -56,8 +57,8 @@ fun AuthWrapper(dao: AppDao, ) {
|
||||
}
|
||||
val googleSignInClient = remember { GoogleSignIn.getClient(context, gso) }
|
||||
var firebaseUser by remember { mutableStateOf<FirebaseUser?>(auth.currentUser) }
|
||||
|
||||
val allowedEmails = listOf("marcandre.charest@gmail.com", "everousseau07@gmail.com")
|
||||
|
||||
var isAuthorized by remember { mutableStateOf<Boolean?>(null) }
|
||||
|
||||
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
|
||||
@@ -68,7 +69,6 @@ fun AuthWrapper(dao: AppDao, ) {
|
||||
try {
|
||||
val authResult = auth.signInWithCredential(credential).await()
|
||||
firebaseUser = authResult.user
|
||||
Log.d("Auth", "Connecté à Firebase avec : ${firebaseUser?.email}")
|
||||
} catch (e: Exception) {
|
||||
Log.e("Auth", "Erreur Firebase Auth : ${e.message}")
|
||||
Toast.makeText(context, "Erreur de synchronisation Firebase.", Toast.LENGTH_LONG).show()
|
||||
@@ -77,9 +77,8 @@ fun AuthWrapper(dao: AppDao, ) {
|
||||
} catch (e: ApiException) {
|
||||
Log.e("Auth", "Erreur Google Sign-In : ${e.statusCode}")
|
||||
val msg = when (e.statusCode) {
|
||||
10 -> "Erreur 10 : SHA-1 non reconnu dans Firebase. Assurez-vous d'avoir ajouté le SHA-1 de TOUTES vos clés de signature."
|
||||
10 -> "Erreur 10 : SHA-1 non reconnu. Assurez-vous d'avoir ajouté le SHA-1 de VOS clés."
|
||||
7 -> "Erreur 7 : Problème de réseau."
|
||||
12500 -> "Erreur 12500 : Problème de configuration Google Play Services."
|
||||
else -> "Erreur Google (Code ${e.statusCode})."
|
||||
}
|
||||
Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
|
||||
@@ -90,17 +89,52 @@ fun AuthWrapper(dao: AppDao, ) {
|
||||
auth.signOut()
|
||||
googleSignInClient.signOut().addOnCompleteListener {
|
||||
firebaseUser = null
|
||||
isAuthorized = null
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(firebaseUser) {
|
||||
isAuthorized = null
|
||||
val email = firebaseUser?.email?.trim()?.lowercase()
|
||||
|
||||
if (email != null && email.isNotEmpty()) {
|
||||
Log.d("Auth", "Vérification de l'autorisation pour l'email: '$email'")
|
||||
try {
|
||||
// On spécifie explicitement la base de données "scan-wich"
|
||||
val db = FirebaseFirestore.getInstance("scan-wich")
|
||||
val docRef = db.collection("authorized_users").document(email)
|
||||
val document = docRef.get().await()
|
||||
|
||||
if (document.exists()) {
|
||||
Log.d("Auth", "Accès AUTORISÉ pour '$email'. Document trouvé.")
|
||||
isAuthorized = true
|
||||
} else {
|
||||
Log.w("Auth", "Accès REFUSÉ pour '$email'. Document NON trouvé.")
|
||||
isAuthorized = false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("Auth", "Erreur critique Firestore. Vérifiez les règles de sécurité.", e)
|
||||
isAuthorized = false
|
||||
}
|
||||
} else if (firebaseUser != null) {
|
||||
Log.w("Auth", "L'utilisateur est connecté mais son email est vide.")
|
||||
isAuthorized = false
|
||||
}
|
||||
}
|
||||
|
||||
if (firebaseUser == null) {
|
||||
LoginScreen { launcher.launch(googleSignInClient.signInIntent) }
|
||||
} else {
|
||||
val userEmail = firebaseUser?.email?.lowercase() ?: ""
|
||||
if (allowedEmails.contains(userEmail)) {
|
||||
MainApp(dao = dao, onLogout = onLogout, userId = firebaseUser!!.uid)
|
||||
} else {
|
||||
AccessDeniedScreen(onLogout)
|
||||
when (isAuthorized) {
|
||||
true -> MainApp(dao = dao, onLogout = onLogout, userId = firebaseUser!!.uid)
|
||||
false -> AccessDeniedScreen(onLogout)
|
||||
null -> {
|
||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
CircularProgressIndicator()
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Text("Vérification de l'accès...")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
package com.example.scanwich
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.input.KeyboardType
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.core.content.edit
|
||||
import com.google.firebase.auth.FirebaseAuth
|
||||
import com.google.firebase.firestore.FirebaseFirestore
|
||||
|
||||
@Composable
|
||||
fun SetupScreen(prefs: SharedPreferences, onComplete: () -> Unit) {
|
||||
@@ -31,6 +30,7 @@ fun SetupScreen(prefs: SharedPreferences, onComplete: () -> Unit) {
|
||||
var goal by remember { mutableStateOf(prefs.getString("goal", "Maintenir le poids") ?: "Maintenir le poids") }
|
||||
var isDiabetic by remember { mutableStateOf(prefs.getBoolean("is_diabetic", false)) }
|
||||
|
||||
val context = LocalContext.current
|
||||
val activityLevels = listOf("Sédentaire", "Légèrement actif", "Modérément actif", "Très actif", "Extrêmement actif")
|
||||
val goals = listOf("Maintenir le poids", "Perdre du poids")
|
||||
|
||||
@@ -139,6 +139,12 @@ fun SetupScreen(prefs: SharedPreferences, onComplete: () -> Unit) {
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
val currentUser = FirebaseAuth.getInstance().currentUser
|
||||
if (currentUser == null) {
|
||||
Toast.makeText(context, "Erreur : Vous devez être connecté pour sauvegarder.", Toast.LENGTH_LONG).show()
|
||||
return@Button
|
||||
}
|
||||
|
||||
val ageInt = age.toIntOrNull() ?: 0
|
||||
val height = heightCm.toDoubleOrNull() ?: 0.0
|
||||
var weightKg = weight.toDoubleOrNull() ?: 0.0
|
||||
@@ -160,22 +166,48 @@ fun SetupScreen(prefs: SharedPreferences, onComplete: () -> Unit) {
|
||||
val targetProtein = (targetCals * 0.2 / 4).toInt()
|
||||
val targetFat = (targetCals * 0.3 / 9).toInt()
|
||||
|
||||
prefs.edit {
|
||||
putString("target_calories", targetCals.toString())
|
||||
putString("target_carbs", targetCarbs.toString())
|
||||
putString("target_protein", targetProtein.toString())
|
||||
putString("target_fat", targetFat.toString())
|
||||
putString("weight_kg", weightKg.toString())
|
||||
putString("weight_display", weightDisplay)
|
||||
putBoolean("is_lbs", isLbs)
|
||||
putString("height_cm", heightCm)
|
||||
putBoolean("is_diabetic", isDiabetic)
|
||||
putInt("age", ageInt)
|
||||
putString("gender", gender)
|
||||
putString("activity_level", activityLevel)
|
||||
putString("goal", goal)
|
||||
}
|
||||
onComplete()
|
||||
val userProfile = hashMapOf(
|
||||
"age" to ageInt,
|
||||
"height_cm" to height,
|
||||
"weight_kg" to weightKg,
|
||||
"is_lbs" to isLbs,
|
||||
"gender" to gender,
|
||||
"activity_level" to activityLevel,
|
||||
"goal" to goal,
|
||||
"is_diabetic" to isDiabetic,
|
||||
"target_calories" to targetCals,
|
||||
"target_carbs" to targetCarbs,
|
||||
"target_protein" to targetProtein,
|
||||
"target_fat" to targetFat
|
||||
)
|
||||
|
||||
// On spécifie explicitement la base de données "scan-wich"
|
||||
FirebaseFirestore.getInstance("scan-wich").collection("users").document(currentUser.uid)
|
||||
.set(userProfile)
|
||||
.addOnSuccessListener {
|
||||
Log.d("SetupScreen", "User profile saved to Firestore.")
|
||||
prefs.edit {
|
||||
putString("target_calories", targetCals.toString())
|
||||
putString("target_carbs", targetCarbs.toString())
|
||||
putString("target_protein", targetProtein.toString())
|
||||
putString("target_fat", targetFat.toString())
|
||||
putString("weight_kg", weightKg.toString())
|
||||
putString("weight_display", weightDisplay)
|
||||
putBoolean("is_lbs", isLbs)
|
||||
putString("height_cm", heightCm)
|
||||
putBoolean("is_diabetic", isDiabetic)
|
||||
putInt("age", ageInt)
|
||||
putString("gender", gender)
|
||||
putString("activity_level", activityLevel)
|
||||
putString("goal", goal)
|
||||
}
|
||||
Toast.makeText(context, "Profil sauvegardé sur votre compte !", Toast.LENGTH_SHORT).show()
|
||||
onComplete()
|
||||
}
|
||||
.addOnFailureListener { e ->
|
||||
Log.w("SetupScreen", "Error writing user profile to Firestore", e)
|
||||
Toast.makeText(context, "Erreur de sauvegarde du profil: ${e.message}", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
||||
Reference in New Issue
Block a user