Pap atualizada
This commit is contained in:
@@ -53,7 +53,6 @@ public class LoginActivity extends AppCompatActivity {
|
||||
mAuth = FirebaseAuth.getInstance();
|
||||
db = FirebaseFirestore.getInstance();
|
||||
|
||||
// Forçar login sempre que abrir a app: faz signOut se já existir sessão
|
||||
if (mAuth.getCurrentUser() != null) {
|
||||
mAuth.signOut();
|
||||
}
|
||||
@@ -242,13 +241,11 @@ public class LoginActivity extends AppCompatActivity {
|
||||
.apply();
|
||||
|
||||
if ("guardian".equals(type)) {
|
||||
// Verifies if guardian is already linked to a user
|
||||
java.util.List<String> managedUsers = (java.util.List<String>) document.get("managedUsers");
|
||||
|
||||
if (managedUsers != null && !managedUsers.isEmpty()) {
|
||||
redirectToApp();
|
||||
} else {
|
||||
// If not linked, redirect to enter access code
|
||||
Intent intent = new Intent(LoginActivity.this, InviteCodeActivity.class);
|
||||
intent.putExtra("is_guardian", true);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
@@ -259,37 +256,45 @@ public class LoginActivity extends AppCompatActivity {
|
||||
redirectToApp();
|
||||
}
|
||||
} else {
|
||||
// Se o documento não existir (ex.: registo antigo sem salvar), cria um básico
|
||||
// Se o perfil não existe, cria um com base no modo de login atual.
|
||||
FirebaseUser user = mAuth.getCurrentUser();
|
||||
String email = user != null ? user.getEmail() : "";
|
||||
String name = (email != null && email.contains("@")) ? email.substring(0, email.indexOf("@"))
|
||||
: "Utilizador";
|
||||
String name = (email != null && email.contains("@")) ? email.substring(0, email.indexOf("@")) : "Utilizador";
|
||||
String userType = isGuardianMode ? "guardian" : "user";
|
||||
|
||||
Map<String, Object> userData = new HashMap<>();
|
||||
userData.put("email", email);
|
||||
userData.put("name", name);
|
||||
userData.put("phone", "");
|
||||
userData.put("type", "user");
|
||||
userData.put("type", userType);
|
||||
userData.put("createdAt", System.currentTimeMillis());
|
||||
|
||||
db.collection("users").document(uid)
|
||||
.set(userData)
|
||||
.addOnSuccessListener(aVoid -> {
|
||||
prefs.edit()
|
||||
.putString("user_type", "user")
|
||||
.putString("user_type", userType)
|
||||
.putString("user_name", name)
|
||||
.apply();
|
||||
redirectToApp();
|
||||
|
||||
if ("guardian".equals(userType)) {
|
||||
Intent intent = new Intent(LoginActivity.this, InviteCodeActivity.class);
|
||||
intent.putExtra("is_guardian", true);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
} else {
|
||||
redirectToApp();
|
||||
}
|
||||
})
|
||||
.addOnFailureListener(e -> {
|
||||
Toast.makeText(this, "Erro ao criar perfil: " + e.getMessage(), Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
Toast.makeText(this, "Erro ao criar perfil: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
btnLogin.setEnabled(true);
|
||||
});
|
||||
}
|
||||
})
|
||||
.addOnFailureListener(e -> {
|
||||
Toast.makeText(this, "Erro ao carregar dados", Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(this, "Erro ao carregar dados do utilizador: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
btnLogin.setEnabled(true);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user