o login esta a dar erro
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4
.idea/deploymentTargetSelector.xml
generated
4
.idea/deploymentTargetSelector.xml
generated
@@ -4,10 +4,10 @@
|
||||
<selectionStates>
|
||||
<SelectionState runConfigName="app">
|
||||
<option name="selectionMode" value="DROPDOWN" />
|
||||
<DropdownSelection timestamp="2026-03-10T15:39:12.049361Z">
|
||||
<DropdownSelection timestamp="2026-04-20T13:42:33.235469Z">
|
||||
<Target type="DEFAULT_BOOT">
|
||||
<handle>
|
||||
<DeviceId pluginId="PhysicalDevice" identifier="serial=b93659d0e5dd" />
|
||||
<DeviceId pluginId="LocalEmulator" identifier="path=/Users/230405/.android/avd/Medium_Phone.avd" />
|
||||
</handle>
|
||||
</Target>
|
||||
</DropdownSelection>
|
||||
|
||||
@@ -118,8 +118,10 @@ public class LoginActivity extends AppCompatActivity {
|
||||
mAuth.signInWithEmailAndPassword(email, pass)
|
||||
.addOnCompleteListener(this, task -> {
|
||||
if (task.isSuccessful()) {
|
||||
startActivity(new Intent(LoginActivity.this, MainActivity.class));
|
||||
finish();
|
||||
FirebaseUser user = mAuth.getCurrentUser();
|
||||
if (user != null) {
|
||||
handleSuccessfulAuth(user, email, pass, true);
|
||||
}
|
||||
} else {
|
||||
binding.loginButton.setEnabled(true);
|
||||
binding.loginButton.setText(R.string.login_button);
|
||||
@@ -132,24 +134,6 @@ public class LoginActivity extends AppCompatActivity {
|
||||
String email = binding.emailEditText.getText().toString();
|
||||
String password = binding.passwordEditText.getText().toString();
|
||||
|
||||
/*
|
||||
* if (email.equals("admin") && password.equals("123")) {
|
||||
* SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
||||
* prefs.edit().putBoolean("is_logged_in", true).apply();
|
||||
* // Mock data for admin
|
||||
* String adminEmail = "admin@papcuida.com";
|
||||
* prefs.edit().putString("user_email", adminEmail).apply();
|
||||
* prefs.edit().putString("user_name", "Administrador").apply();
|
||||
*
|
||||
* // Remove local Room admin creation since Room is gone
|
||||
*
|
||||
* Toast.makeText(this, "Login de Administrador", Toast.LENGTH_SHORT).show();
|
||||
* startActivity(new Intent(this, MainActivity.class));
|
||||
* finish();
|
||||
* return;
|
||||
* }
|
||||
*/
|
||||
|
||||
if (email.isEmpty() || password.isEmpty()) {
|
||||
Toast.makeText(this, "Preencha todos os campos", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
@@ -160,75 +144,11 @@ public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
mAuth.signInWithEmailAndPassword(email, password)
|
||||
.addOnCompleteListener(this, task -> {
|
||||
binding.loginButton.setEnabled(true);
|
||||
binding.loginButton.setText(R.string.login_button);
|
||||
|
||||
if (task.isSuccessful()) {
|
||||
// Sign in success, fetch from Firestore
|
||||
FirebaseUser user = mAuth.getCurrentUser();
|
||||
if (user != null) {
|
||||
android.util.Log.d("LOGIN_DEBUG", "User authenticated: " + user.getUid());
|
||||
com.google.firebase.firestore.FirebaseFirestore.getInstance()
|
||||
.collection("utilizadores")
|
||||
.document(user.getUid())
|
||||
.get(com.google.firebase.firestore.Source.DEFAULT) // Try cache if offline
|
||||
.addOnCompleteListener(fetchTask -> {
|
||||
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
||||
boolean rememberMe = binding.checkboxRememberMe.isChecked();
|
||||
|
||||
prefs.edit().putBoolean("is_logged_in", true).apply();
|
||||
prefs.edit().putBoolean("remember_me", rememberMe).apply();
|
||||
|
||||
// Guardar sempre para biometria após login manual com sucesso
|
||||
prefs.edit().putString("saved_email", email).apply();
|
||||
prefs.edit().putString("saved_pass", password).apply();
|
||||
|
||||
if (fetchTask.isSuccessful() && fetchTask.getResult() != null
|
||||
&& fetchTask.getResult().exists()) {
|
||||
com.google.firebase.firestore.DocumentSnapshot documentSnapshot = fetchTask
|
||||
.getResult();
|
||||
android.util.Log.d("LOGIN_DEBUG",
|
||||
"Firestore document fetched: " + documentSnapshot.exists());
|
||||
|
||||
String name = documentSnapshot.getString("name");
|
||||
String dbEmail = documentSnapshot.getString("email");
|
||||
android.util.Log.d("LOGIN_DEBUG",
|
||||
"Firestore Data -> Name: " + name + " Email: " + dbEmail);
|
||||
|
||||
if (name != null)
|
||||
prefs.edit().putString("user_name", name).apply();
|
||||
if (dbEmail != null)
|
||||
prefs.edit().putString("user_email", dbEmail).apply();
|
||||
} else {
|
||||
android.util.Log.d("LOGIN_DEBUG",
|
||||
"Firestore document missing or offline. Falling back...");
|
||||
if (!fetchTask.isSuccessful()) {
|
||||
android.util.Log.e("LOGIN_DEBUG", "Firestore fetch error",
|
||||
fetchTask.getException());
|
||||
}
|
||||
// Fallback to Auth data
|
||||
prefs.edit().putString("user_email", user.getEmail()).apply();
|
||||
if (user.getDisplayName() != null && !user.getDisplayName().isEmpty()) {
|
||||
prefs.edit().putString("user_name", user.getDisplayName()).apply();
|
||||
} else {
|
||||
// If even Auth display name is empty, fetch name from email prefix as a
|
||||
// last resort
|
||||
String authEmail = user.getEmail();
|
||||
if (authEmail != null && authEmail.contains("@")) {
|
||||
String fallbackName = authEmail.substring(0,
|
||||
authEmail.indexOf("@"));
|
||||
prefs.edit().putString("user_name", fallbackName).apply();
|
||||
}
|
||||
}
|
||||
Toast.makeText(LoginActivity.this,
|
||||
"Aviso: A iniciar sessão offline ou sem perfil completo.",
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
Toast.makeText(LoginActivity.this, "Bem-vindo!", Toast.LENGTH_SHORT).show();
|
||||
startActivity(new Intent(LoginActivity.this, MainActivity.class));
|
||||
finish();
|
||||
});
|
||||
boolean rememberMe = binding.checkboxRememberMe.isChecked();
|
||||
handleSuccessfulAuth(user, email, password, rememberMe);
|
||||
}
|
||||
} else {
|
||||
binding.loginButton.setEnabled(true);
|
||||
@@ -251,4 +171,64 @@ public class LoginActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleSuccessfulAuth(FirebaseUser user, String email, String password, boolean rememberMe) {
|
||||
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
||||
prefs.edit().putBoolean("is_logged_in", true).apply();
|
||||
prefs.edit().putBoolean("remember_me", rememberMe).apply();
|
||||
|
||||
// Guardar sempre para biometria após login com sucesso
|
||||
prefs.edit().putString("saved_email", email).apply();
|
||||
prefs.edit().putString("saved_pass", password).apply();
|
||||
|
||||
// Tentar primeiro na coleção 'utilizadores'
|
||||
com.google.firebase.firestore.FirebaseFirestore db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
|
||||
db.collection("utilizadores").document(user.getUid()).get()
|
||||
.addOnCompleteListener(task -> {
|
||||
if (task.isSuccessful() && task.getResult() != null && task.getResult().exists()) {
|
||||
com.google.firebase.firestore.DocumentSnapshot doc = task.getResult();
|
||||
String name = doc.getString("name");
|
||||
String dbEmail = doc.getString("email");
|
||||
|
||||
if (name != null) prefs.edit().putString("user_name", name).apply();
|
||||
if (dbEmail != null) prefs.edit().putString("user_email", dbEmail).apply();
|
||||
|
||||
proceedToMain();
|
||||
} else {
|
||||
// Tentar na coleção 'medicos'
|
||||
db.collection("medicos").document(user.getUid()).get()
|
||||
.addOnCompleteListener(task2 -> {
|
||||
if (task2.isSuccessful() && task2.getResult() != null && task2.getResult().exists()) {
|
||||
com.google.firebase.firestore.DocumentSnapshot doc = task2.getResult();
|
||||
String name = doc.getString("nome"); // Notar campo 'nome' em vez de 'name'
|
||||
String dbEmail = doc.getString("email");
|
||||
|
||||
if (name != null) prefs.edit().putString("user_name", name).apply();
|
||||
if (dbEmail != null) prefs.edit().putString("user_email", dbEmail).apply();
|
||||
|
||||
proceedToMain();
|
||||
} else {
|
||||
// Fallback se não encontrar em lado nenhum
|
||||
prefs.edit().putString("user_email", user.getEmail()).apply();
|
||||
if (user.getDisplayName() != null && !user.getDisplayName().isEmpty()) {
|
||||
prefs.edit().putString("user_name", user.getDisplayName()).apply();
|
||||
} else {
|
||||
String authEmail = user.getEmail();
|
||||
if (authEmail != null && authEmail.contains("@")) {
|
||||
String fallbackName = authEmail.substring(0, authEmail.indexOf("@"));
|
||||
prefs.edit().putString("user_name", fallbackName).apply();
|
||||
}
|
||||
}
|
||||
proceedToMain();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void proceedToMain() {
|
||||
Toast.makeText(LoginActivity.this, "Bem-vindo!", Toast.LENGTH_SHORT).show();
|
||||
startActivity(new Intent(LoginActivity.this, com.example.cuida.MainActivity.class));
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user