..asd
This commit is contained in:
@@ -41,7 +41,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
mAuth = FirebaseAuth.getInstance();
|
mAuth = FirebaseAuth.getInstance();
|
||||||
db = FirebaseFirestore.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) {
|
if (mAuth.getCurrentUser() != null) {
|
||||||
checkUserRoleAndRedirect(mAuth.getCurrentUser().getUid());
|
checkUserRoleAndRedirect(mAuth.getCurrentUser().getUid());
|
||||||
@@ -72,27 +72,51 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private void checkUserRoleAndRedirect(String uid) {
|
private void checkUserRoleAndRedirect(String uid) {
|
||||||
if (progressBar != null) progressBar.setVisibility(View.VISIBLE);
|
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 -> {
|
db.collection("Clientes").document(uid).get().addOnSuccessListener(docClient -> {
|
||||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
|
||||||
if (docClient.exists()) {
|
if (docClient.exists()) {
|
||||||
|
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||||
startActivity(new Intent(this, ClientDashboardActivity.class));
|
startActivity(new Intent(this, ClientDashboardActivity.class));
|
||||||
finish();
|
finish();
|
||||||
} else {
|
} else {
|
||||||
// Check if it's a Restaurant to give a better error message
|
// 2. Try legacy users collection
|
||||||
db.collection("Restaurantes").document(uid).get().addOnSuccessListener(docRest -> {
|
db.collection("users").document(uid).get().addOnSuccessListener(docLegacy -> {
|
||||||
if (docRest.exists()) {
|
if (docLegacy.exists()) {
|
||||||
Toast.makeText(this, "Contas de Restaurante devem usar o Dashboard Web.", Toast.LENGTH_LONG).show();
|
// 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 {
|
||||||
|
handleNoClientAccess(uid);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, "Conta de Cliente não encontrada.", Toast.LENGTH_LONG).show();
|
handleNoClientAccess(uid);
|
||||||
}
|
}
|
||||||
mAuth.signOut();
|
|
||||||
initViews();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).addOnFailureListener(e -> {
|
}).addOnFailureListener(e -> {
|
||||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
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();
|
initViews();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -103,7 +127,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
String name = inputName.getText().toString().trim();
|
String name = inputName.getText().toString().trim();
|
||||||
|
|
||||||
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +138,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
.addOnSuccessListener(authResult -> checkUserRoleAndRedirect(authResult.getUser().getUid()))
|
.addOnSuccessListener(authResult -> checkUserRoleAndRedirect(authResult.getUser().getUid()))
|
||||||
.addOnFailureListener(e -> {
|
.addOnFailureListener(e -> {
|
||||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
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 {
|
} else {
|
||||||
if (TextUtils.isEmpty(name)) {
|
if (TextUtils.isEmpty(name)) {
|
||||||
@@ -128,13 +152,14 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
User newUser = new User(uid, name, email, "CLIENTE");
|
User newUser = new User(uid, name, email, "CLIENTE");
|
||||||
db.collection("Clientes").document(uid).set(newUser)
|
db.collection("Clientes").document(uid).set(newUser)
|
||||||
.addOnSuccessListener(aVoid -> {
|
.addOnSuccessListener(aVoid -> {
|
||||||
|
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
||||||
startActivity(new Intent(this, ClientDashboardActivity.class));
|
startActivity(new Intent(this, ClientDashboardActivity.class));
|
||||||
finish();
|
finish();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.addOnFailureListener(e -> {
|
.addOnFailureListener(e -> {
|
||||||
if (progressBar != null) progressBar.setVisibility(View.GONE);
|
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 {
|
public class User {
|
||||||
private String id;
|
private String id;
|
||||||
private String name;
|
private String displayName;
|
||||||
private String email;
|
private String email;
|
||||||
private String photoURL;
|
private String photoURL;
|
||||||
private String role; // "CLIENTE" or "RESTAURANTE"
|
private String role; // "CLIENTE" or "RESTAURANTE"
|
||||||
@@ -13,9 +13,9 @@ public class User {
|
|||||||
|
|
||||||
public 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.id = id;
|
||||||
this.name = name;
|
this.displayName = displayName;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.role = role;
|
this.role = role;
|
||||||
this.createdAt = new Timestamp(new Date());
|
this.createdAt = new Timestamp(new Date());
|
||||||
@@ -23,8 +23,8 @@ public class User {
|
|||||||
|
|
||||||
public String getId() { return id; }
|
public String getId() { return id; }
|
||||||
public void setId(String id) { this.id = id; }
|
public void setId(String id) { this.id = id; }
|
||||||
public String getName() { return name; }
|
public String getDisplayName() { return displayName; }
|
||||||
public void setName(String name) { this.name = name; }
|
public void setDisplayName(String displayName) { this.displayName = displayName; }
|
||||||
public String getEmail() { return email; }
|
public String getEmail() { return email; }
|
||||||
public void setEmail(String email) { this.email = email; }
|
public void setEmail(String email) { this.email = email; }
|
||||||
public String getPhotoURL() { return photoURL; }
|
public String getPhotoURL() { return photoURL; }
|
||||||
|
|||||||
Reference in New Issue
Block a user