diff --git a/src/main/java/org/to/telegramfinalproject/UI/RegisterController.java b/src/main/java/org/to/telegramfinalproject/UI/RegisterController.java index afc2174..e920f2c 100644 --- a/src/main/java/org/to/telegramfinalproject/UI/RegisterController.java +++ b/src/main/java/org/to/telegramfinalproject/UI/RegisterController.java @@ -40,18 +40,16 @@ 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 + @FXML private void togglePasswordVisibility() { passwordVisible = !passwordVisible; visiblePasswordField.setVisible(passwordVisible); @@ -71,66 +69,149 @@ 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 isn’t 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(); - String username = usernameField.getText().trim(); + String userID = userIdField.getText().trim(); + String username = usernameField.getText().trim(); String profileName = profileNameField.getText().trim(); - String password = passwordField.getText(); + 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; + 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; - } + new Thread(() -> { + try { + // 1) مطمئن شو کانکشن/لیسنر روشن است + var cli = org.to.telegramfinalproject.Client.TelegramClient.getOrInitForUI(); + var handler = cli.getHandler(); - // 4. Password mismatch - if (!password.equals(confirmPass)) { - showError("Passwords do not match."); - return; - } + // 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); - // 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; - } + 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; + } - // 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()); + // 3) Auto-Login با همان کانکشن + handler.login(username, password); - // Simulate successful registration (since main.fxml isn’t ready) - Alert alert = new Alert(Alert.AlertType.INFORMATION, "Registration successful!"); - alert.show(); + 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."); - } + } catch (Exception ex) { + 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();