Connect register to main

This commit is contained in:
2025-08-30 23:21:33 +03:30
parent 46216d3368
commit 71ca5b5ff4
@@ -40,15 +40,13 @@ public class RegisterController {
@FXML
public void initialize() {
// Sync password and confirm fields with their visible counterparts
visiblePasswordField.textProperty().bindBidirectional(passwordField.textProperty());
visibleConfirmPasswordField.textProperty().bindBidirectional(confirmPasswordField.textProperty());
try {
connection = new ClientConnection("localhost", 8000);
} catch (Exception e) {
System.out.println("Could not connect to server: " + e.getMessage());
org.to.telegramfinalproject.Client.TelegramClient.getOrInitForUI();
} catch (Exception ignored) {
}
visiblePasswordField.textProperty().bindBidirectional(passwordField.textProperty());
visibleConfirmPasswordField.textProperty().bindBidirectional(confirmPasswordField.textProperty());
}
@FXML
@@ -71,6 +69,68 @@ public class RegisterController {
toggleConfirmBtn.setText(confirmVisible ? "👁" : "👁");
}
// @FXML
// private void handleRegister() {
// String userID = userIdField.getText().trim();
// String username = usernameField.getText().trim();
// String profileName = profileNameField.getText().trim();
// String password = passwordField.getText();
// String confirmPass = confirmPasswordField.getText();
//
// userDatabase userDb = new userDatabase();
// String passwordRegex = "\\b(?=[^\\s]*[A-Z])(?=[^\\s]*[a-z])(?=[^\\s]*\\d)(?=[^\\s]*[!@#$%^&*])[^\\s]{8,}\\b";
//
// // 1. Empty fields
// if (userID.isEmpty() || username.isEmpty() || profileName.isEmpty() || password.isEmpty() || confirmPass.isEmpty()) {
// showError("Please fill in all required fields.");
// return;
// }
//
// // 2. Username exists
// if (userDb.existsByUsername(username)) {
// showError("This username is already taken.");
// return;
// }
//
// // 3. User ID exists
// if (userDb.existsByUserId(userID)) {
// showError("This user ID is already taken.");
// return;
// }
//
// // 4. Password mismatch
// if (!password.equals(confirmPass)) {
// showError("Passwords do not match.");
// return;
// }
//
// // 5. Weak password
// if (!password.matches(passwordRegex)) {
// showError("Password must be at least 8 characters, include a capital letter, a number, and a special character.");
// return;
// }
//
// // 6. Attempt registration
// try {
// JSONObject request = new JSONObject();
// request.put("action", "register");
// request.put("user_id", userID);
// request.put("username", username);
// request.put("password", password);
// request.put("profile_name", profileName);
// connection.send(request.toString());
//
// // Simulate successful registration (since main.fxml isnt ready)
// Alert alert = new Alert(Alert.AlertType.INFORMATION, "Registration successful!");
// alert.show();
//
// } catch (Exception ex) {
// showError("Failed to register. Please try again later.");
// }
// }
@FXML
private void handleRegister() {
String userID = userIdField.getText().trim();
@@ -82,55 +142,76 @@ public class RegisterController {
userDatabase userDb = new userDatabase();
String passwordRegex = "\\b(?=[^\\s]*[A-Z])(?=[^\\s]*[a-z])(?=[^\\s]*\\d)(?=[^\\s]*[!@#$%^&*])[^\\s]{8,}\\b";
// 1. Empty fields
// اعتبارسنجی‌ها
if (userID.isEmpty() || username.isEmpty() || profileName.isEmpty() || password.isEmpty() || confirmPass.isEmpty()) {
showError("Please fill in all required fields.");
return;
showError("Please fill in all required fields."); return;
}
if (userDb.existsByUsername(username)) { showError("This username is already taken."); return; }
if (userDb.existsByUserId(userID)) { showError("This user ID is already taken."); return; }
if (!password.equals(confirmPass)) { showError("Passwords do not match."); return; }
if (!password.matches(passwordRegex)) { showError("Password must be at least 8 characters, include a capital letter, a number, and a special character."); return; }
// 2. Username exists
if (userDb.existsByUsername(username)) {
showError("This username is already taken.");
return;
}
// UI را موقتاً disable کن (اختیاری)
setBusy(true);
// 3. User ID exists
if (userDb.existsByUserId(userID)) {
showError("This user ID is already taken.");
return;
}
// 4. Password mismatch
if (!password.equals(confirmPass)) {
showError("Passwords do not match.");
return;
}
// 5. Weak password
if (!password.matches(passwordRegex)) {
showError("Password must be at least 8 characters, include a capital letter, a number, and a special character.");
return;
}
// 6. Attempt registration
new Thread(() -> {
try {
JSONObject request = new JSONObject();
request.put("action", "register");
request.put("user_id", userID);
request.put("username", username);
request.put("password", password);
request.put("profile_name", profileName);
connection.send(request.toString());
// 1) مطمئن شو کانکشن/لیسنر روشن است
var cli = org.to.telegramfinalproject.Client.TelegramClient.getOrInitForUI();
var handler = cli.getHandler();
// Simulate successful registration (since main.fxml isnt ready)
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Registration successful!");
alert.show();
// 2) Register
var regReq = new org.json.JSONObject()
.put("action", "register")
.put("user_id", userID)
.put("username", username)
.put("password", password)
.put("profile_name", profileName);
var regResp = org.to.telegramfinalproject.Client.ActionHandler.sendWithResponse(regReq);
if (regResp == null || !"success".equalsIgnoreCase(regResp.optString("status"))) {
javafx.application.Platform.runLater(() -> {
setBusy(false);
showError(regResp != null ? regResp.optString("message","Register failed.") : "Register failed.");
});
return;
}
// 3) Auto-Login با همان کانکشن
handler.login(username, password);
javafx.application.Platform.runLater(() -> {
setBusy(false);
if (org.to.telegramfinalproject.Client.Session.currentUser == null || !handler.wasSuccess()) {
showError(handler.getLastMessage().isEmpty() ? "Login failed." : handler.getLastMessage());
return;
}
org.to.telegramfinalproject.UI.AppRouter.showMain();
});
} catch (Exception ex) {
showError("Failed to register. Please try again later.");
javafx.application.Platform.runLater(() -> {
setBusy(false);
showError("Failed to register/login: " + ex.getMessage());
});
}
}, "register-thread").start();
}
private void setBusy(boolean b){
userIdField.setDisable(b);
usernameField.setDisable(b);
profileNameField.setDisable(b);
passwordField.setDisable(b);
visiblePasswordField.setDisable(b);
confirmPasswordField.setDisable(b);
visibleConfirmPasswordField.setDisable(b);
togglePasswordBtn.setDisable(b);
toggleConfirmBtn.setDisable(b);
}
@FXML
private void switchToLogin() throws IOException {
showLogin();