Connect backend to UI
This commit is contained in:
@@ -9,7 +9,10 @@ import javafx.scene.control.*;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.util.Duration;
|
||||
import org.json.JSONObject;
|
||||
import org.to.telegramfinalproject.Client.ActionHandler;
|
||||
import org.to.telegramfinalproject.Client.ClientConnection;
|
||||
import org.to.telegramfinalproject.Client.Session;
|
||||
import org.to.telegramfinalproject.Client.TelegramClient;
|
||||
import org.to.telegramfinalproject.Database.userDatabase;
|
||||
import org.to.telegramfinalproject.Models.User;
|
||||
import org.to.telegramfinalproject.Security.PasswordHashing;
|
||||
@@ -23,7 +26,7 @@ public class LoginController {
|
||||
@FXML private PasswordField passwordField;
|
||||
@FXML private TextField visiblePasswordField;
|
||||
|
||||
@FXML private Button togglePasswordBtn;
|
||||
@FXML private Button toggleVisibilityBtn;
|
||||
@FXML private Label errorLabel;
|
||||
|
||||
private ClientConnection connection;
|
||||
@@ -48,60 +51,119 @@ public class LoginController {
|
||||
visiblePasswordField.setManaged(passwordVisible);
|
||||
passwordField.setVisible(!passwordVisible);
|
||||
passwordField.setManaged(!passwordVisible);
|
||||
togglePasswordBtn.setText(passwordVisible ? "👁" : "👁");
|
||||
toggleVisibilityBtn.setText(passwordVisible ? "👁" : "👁");
|
||||
}
|
||||
|
||||
// @FXML
|
||||
// private void handleLogin() {
|
||||
// String username = usernameField.getText();
|
||||
// String password = passwordField.getText();
|
||||
//
|
||||
// // 1. Check for empty fields
|
||||
// if (username.isEmpty() || password.isEmpty()) {
|
||||
// showError("Please fill in all required fields.");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // 2. Check if username exists
|
||||
// userDatabase userDb = new userDatabase();
|
||||
// if (!userDb.existsByUsername(username)) {
|
||||
// showError("This username doesn’t exist.");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // 3. Check if password is correct
|
||||
// User user = userDb.findByUsername(username);
|
||||
// if (!PasswordHashing.verify(password, user.getPassword())) {
|
||||
// showError("Incorrect password.");
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // 4. Attempt to send to server
|
||||
// try {
|
||||
// JSONObject request = new JSONObject();
|
||||
// request.put("action", "login");
|
||||
// request.put("user_id", JSONObject.NULL);
|
||||
// request.put("username", username);
|
||||
// request.put("password", password);
|
||||
// request.put("profile_name", JSONObject.NULL);
|
||||
//
|
||||
// if (connection != null) {
|
||||
// connection.send(request.toString());
|
||||
// String responseStr = connection.receive();
|
||||
// JSONObject response = new JSONObject(responseStr);
|
||||
// System.out.println("Status: " + response.getString("status"));
|
||||
// System.out.println("Message: " + response.getString("message"));
|
||||
//
|
||||
// // Simulate successful login (since main.fxml isn’t ready)
|
||||
// Alert alert = new Alert(Alert.AlertType.INFORMATION, "Login successful!");
|
||||
// alert.show();
|
||||
// }
|
||||
//
|
||||
// } catch (Exception ex) {
|
||||
// showError("Unable to connect to server. Please try again later.");
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// LoginController.java (متد اصلی)
|
||||
@FXML
|
||||
private void handleLogin() {
|
||||
String username = usernameField.getText();
|
||||
String password = passwordField.getText();
|
||||
String u = usernameField.getText().trim();
|
||||
String p = passwordField.getText();
|
||||
if (u.isEmpty() || p.isEmpty()) { showError("Please fill in all required fields."); return; }
|
||||
|
||||
// 1. Check for empty fields
|
||||
if (username.isEmpty() || password.isEmpty()) {
|
||||
showError("Please fill in all required fields.");
|
||||
return;
|
||||
}
|
||||
setUiBusy(true);
|
||||
|
||||
// 2. Check if username exists
|
||||
userDatabase userDb = new userDatabase();
|
||||
if (!userDb.existsByUsername(username)) {
|
||||
showError("This username doesn’t exist.");
|
||||
return;
|
||||
}
|
||||
new Thread(() -> {
|
||||
try {
|
||||
var cli = org.to.telegramfinalproject.Client.TelegramClient.getOrInitForUI();
|
||||
var handler = cli.getHandler();
|
||||
|
||||
// 3. Check if password is correct
|
||||
User user = userDb.findByUsername(username);
|
||||
if (!PasswordHashing.verify(password, user.getPassword())) {
|
||||
showError("Incorrect password.");
|
||||
return;
|
||||
}
|
||||
// بساز و بفرست — همون send خودت که Session رو پر میکند
|
||||
// org.json.JSONObject req = new org.json.JSONObject()
|
||||
// .put("action","login")
|
||||
// .put("username", u)
|
||||
// .put("password", p);
|
||||
//
|
||||
// handler.send(req); // ⬅️ بلاکینگ؛ پس درستش کردیم که تو Thread هست
|
||||
|
||||
// 4. Attempt to send to server
|
||||
try {
|
||||
JSONObject request = new JSONObject();
|
||||
request.put("action", "login");
|
||||
request.put("user_id", JSONObject.NULL);
|
||||
request.put("username", username);
|
||||
request.put("password", password);
|
||||
request.put("profile_name", JSONObject.NULL);
|
||||
|
||||
if (connection != null) {
|
||||
connection.send(request.toString());
|
||||
String responseStr = connection.receive();
|
||||
JSONObject response = new JSONObject(responseStr);
|
||||
System.out.println("Status: " + response.getString("status"));
|
||||
System.out.println("Message: " + response.getString("message"));
|
||||
handler.login(u,p);
|
||||
|
||||
// Simulate successful login (since main.fxml isn’t ready)
|
||||
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Login successful!");
|
||||
alert.show();
|
||||
javafx.application.Platform.runLater(() -> {
|
||||
setUiBusy(false);
|
||||
|
||||
if (!handler.wasSuccess() || Session.currentUser == null) {
|
||||
showError(handler.getLastMessage().isEmpty() ? "Login failed." : handler.getLastMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
// موفق: Session از داخل send پر شده
|
||||
goMain(); // بدون Alert → مستقیم به main.fxml
|
||||
});
|
||||
|
||||
} catch (Exception ex) {
|
||||
javafx.application.Platform.runLater(() -> {
|
||||
setUiBusy(false);
|
||||
showError("Connection error.");
|
||||
});
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
showError("Unable to connect to server. Please try again later.");
|
||||
}
|
||||
}, "login-thread").start();
|
||||
}
|
||||
|
||||
private void goMain() {
|
||||
AppRouter.showMain();
|
||||
}
|
||||
|
||||
|
||||
private void setUiBusy(boolean b) {
|
||||
usernameField.setDisable(b);
|
||||
passwordField.setDisable(b);
|
||||
}
|
||||
|
||||
|
||||
@FXML
|
||||
private void switchToRegister() throws IOException {
|
||||
switchScene("register_view.fxml");
|
||||
|
||||
Reference in New Issue
Block a user