Connect register to main
This commit is contained in:
@@ -40,15 +40,13 @@ public class RegisterController {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
// Sync password and confirm fields with their visible counterparts
|
|
||||||
visiblePasswordField.textProperty().bindBidirectional(passwordField.textProperty());
|
|
||||||
visibleConfirmPasswordField.textProperty().bindBidirectional(confirmPasswordField.textProperty());
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
connection = new ClientConnection("localhost", 8000);
|
org.to.telegramfinalproject.Client.TelegramClient.getOrInitForUI();
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
System.out.println("Could not connect to server: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
visiblePasswordField.textProperty().bindBidirectional(passwordField.textProperty());
|
||||||
|
visibleConfirmPasswordField.textProperty().bindBidirectional(confirmPasswordField.textProperty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -71,6 +69,68 @@ public class RegisterController {
|
|||||||
toggleConfirmBtn.setText(confirmVisible ? "👁" : "👁");
|
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
|
@FXML
|
||||||
private void handleRegister() {
|
private void handleRegister() {
|
||||||
String userID = userIdField.getText().trim();
|
String userID = userIdField.getText().trim();
|
||||||
@@ -82,55 +142,76 @@ public class RegisterController {
|
|||||||
userDatabase userDb = new userDatabase();
|
userDatabase userDb = new userDatabase();
|
||||||
String passwordRegex = "\\b(?=[^\\s]*[A-Z])(?=[^\\s]*[a-z])(?=[^\\s]*\\d)(?=[^\\s]*[!@#$%^&*])[^\\s]{8,}\\b";
|
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()) {
|
if (userID.isEmpty() || username.isEmpty() || profileName.isEmpty() || password.isEmpty() || confirmPass.isEmpty()) {
|
||||||
showError("Please fill in all required fields.");
|
showError("Please fill in all required fields."); return;
|
||||||
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
|
// UI را موقتاً disable کن (اختیاری)
|
||||||
if (userDb.existsByUsername(username)) {
|
setBusy(true);
|
||||||
showError("This username is already taken.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. User ID exists
|
new Thread(() -> {
|
||||||
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 {
|
try {
|
||||||
JSONObject request = new JSONObject();
|
// 1) مطمئن شو کانکشن/لیسنر روشن است
|
||||||
request.put("action", "register");
|
var cli = org.to.telegramfinalproject.Client.TelegramClient.getOrInitForUI();
|
||||||
request.put("user_id", userID);
|
var handler = cli.getHandler();
|
||||||
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)
|
// 2) Register
|
||||||
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Registration successful!");
|
var regReq = new org.json.JSONObject()
|
||||||
alert.show();
|
.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) {
|
} 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
|
@FXML
|
||||||
private void switchToLogin() throws IOException {
|
private void switchToLogin() throws IOException {
|
||||||
showLogin();
|
showLogin();
|
||||||
|
|||||||
Reference in New Issue
Block a user