..asd
This commit is contained in:
@@ -41,7 +41,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
mAuth = FirebaseAuth.getInstance();
|
||||
db = FirebaseFirestore.getInstance();
|
||||
progressBar = findViewById(R.id.progressBarLogin); // Assuming it exists or I'll add it
|
||||
progressBar = findViewById(R.id.progressBarLogin);
|
||||
|
||||
if (mAuth.getCurrentUser() != null) {
|
||||
checkUserRoleAndRedirect(mAuth.getCurrentUser().getUid());
|
||||
@@ -72,27 +72,51 @@ public class MainActivity extends AppCompatActivity {
|
||||
private void checkUserRoleAndRedirect(String uid) {
|
||||
if (progressBar != null) progressBar.setVisibility(View.VISIBLE);
|
||||
|
||||
// ONLY allow Clientes in the mobile app
|
||||
// 1. Try Clientes
|
||||
db.collection("Clientes").document(uid).get().addOnSuccessListener(docClient -> {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
if (docClient.exists()) {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
startActivity(new Intent(this, ClientDashboardActivity.class));
|
||||
finish();
|
||||
} else {
|
||||
// Check if it's a Restaurant to give a better error message
|
||||
db.collection("Restaurantes").document(uid).get().addOnSuccessListener(docRest -> {
|
||||
if (docRest.exists()) {
|
||||
Toast.makeText(this, "Contas de Restaurante devem usar o Dashboard Web.", Toast.LENGTH_LONG).show();
|
||||
// 2. Try legacy users collection
|
||||
db.collection("users").document(uid).get().addOnSuccessListener(docLegacy -> {
|
||||
if (docLegacy.exists()) {
|
||||
// Migrate to Clientes
|
||||
User legacyUser = docLegacy.toObject(User.class);
|
||||
if (legacyUser != null && "CLIENTE".equalsIgnoreCase(legacyUser.getRole())) {
|
||||
db.collection("Clientes").document(uid).set(legacyUser)
|
||||
.addOnSuccessListener(aVoid -> {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
startActivity(new Intent(this, ClientDashboardActivity.class));
|
||||
finish();
|
||||
});
|
||||
} else {
|
||||
Toast.makeText(this, "Conta de Cliente não encontrada.", Toast.LENGTH_LONG).show();
|
||||
handleNoClientAccess(uid);
|
||||
}
|
||||
} else {
|
||||
handleNoClientAccess(uid);
|
||||
}
|
||||
mAuth.signOut();
|
||||
initViews();
|
||||
});
|
||||
}
|
||||
}).addOnFailureListener(e -> {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
Toast.makeText(this, "Erro ao verificar conta: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(this, "Erro de rede ou permissões. Verifique a sua ligação.", Toast.LENGTH_LONG).show();
|
||||
initViews();
|
||||
});
|
||||
initViews();
|
||||
}
|
||||
|
||||
private void handleNoClientAccess(String uid) {
|
||||
// Check if it's a Restaurant
|
||||
db.collection("Restaurantes").document(uid).get().addOnSuccessListener(docRest -> {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
if (docRest.exists()) {
|
||||
Toast.makeText(this, "Contas de Restaurante devem usar o Dashboard Web.", Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(this, "Utilizador não encontrado. Por favor, crie uma conta.", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
mAuth.signOut();
|
||||
initViews();
|
||||
});
|
||||
}
|
||||
@@ -103,7 +127,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
String name = inputName.getText().toString().trim();
|
||||
|
||||
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
|
||||
Toast.makeText(this, "Preencha os campos obrigatorios.", Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(this, "Preencha todos os campos.", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,7 +138,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
.addOnSuccessListener(authResult -> checkUserRoleAndRedirect(authResult.getUser().getUid()))
|
||||
.addOnFailureListener(e -> {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
Toast.makeText(this, "Falha no login: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(this, "Login falhou: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
});
|
||||
} else {
|
||||
if (TextUtils.isEmpty(name)) {
|
||||
@@ -128,13 +152,14 @@ public class MainActivity extends AppCompatActivity {
|
||||
User newUser = new User(uid, name, email, "CLIENTE");
|
||||
db.collection("Clientes").document(uid).set(newUser)
|
||||
.addOnSuccessListener(aVoid -> {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
startActivity(new Intent(this, ClientDashboardActivity.class));
|
||||
finish();
|
||||
});
|
||||
})
|
||||
.addOnFailureListener(e -> {
|
||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||
Toast.makeText(this, "Erro no registo: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(this, "Registo falhou: " + e.getMessage(), Toast.LENGTH_LONG).show();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.Date;
|
||||
|
||||
public class User {
|
||||
private String id;
|
||||
private String name;
|
||||
private String displayName;
|
||||
private String email;
|
||||
private String photoURL;
|
||||
private String role; // "CLIENTE" or "RESTAURANTE"
|
||||
@@ -13,9 +13,9 @@ public class User {
|
||||
|
||||
public User() {}
|
||||
|
||||
public User(String id, String name, String email, String role) {
|
||||
public User(String id, String displayName, String email, String role) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.displayName = displayName;
|
||||
this.email = email;
|
||||
this.role = role;
|
||||
this.createdAt = new Timestamp(new Date());
|
||||
@@ -23,8 +23,8 @@ public class User {
|
||||
|
||||
public String getId() { return id; }
|
||||
public void setId(String id) { this.id = id; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public String getDisplayName() { return displayName; }
|
||||
public void setDisplayName(String displayName) { this.displayName = displayName; }
|
||||
public String getEmail() { return email; }
|
||||
public void setEmail(String email) { this.email = email; }
|
||||
public String getPhotoURL() { return photoURL; }
|
||||
|
||||
Reference in New Issue
Block a user