From 33505ee49b580b63a876f8f20df2bbf4735ac617 Mon Sep 17 00:00:00 2001
From: AdrianBalunan
Date: Thu, 16 Apr 2026 13:52:25 +0200
Subject: [PATCH] Feat: System can now make a user (+ Fxml for login and
register and controller for register). Login needs handling
---
.../controller/CreateUserPageController.java | 101 ++++++++++
.../controller/DonationPageController.java | 25 ++-
.../team6/controller/LoginPageController.java | 34 ++++
.../team6/service/AuthenticationService.java | 10 +-
.../resources/fxml/creater_user_site.fxml | 184 +-----------------
.../src/main/resources/fxml/loginSite.fxml | 149 +-------------
6 files changed, 171 insertions(+), 332 deletions(-)
create mode 100644 helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java
create mode 100644 helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/LoginPageController.java
diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java
new file mode 100644
index 00000000..acbb442e
--- /dev/null
+++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/CreateUserPageController.java
@@ -0,0 +1,101 @@
+package ntnu.systemutvikling.team6.controller;
+
+import javafx.event.ActionEvent;
+import javafx.fxml.FXML;
+import javafx.scene.control.Alert;
+import javafx.scene.control.PasswordField;
+import javafx.scene.control.TextField;
+import ntnu.systemutvikling.team6.controller.components.BaseController;
+import ntnu.systemutvikling.team6.controller.components.FooterController;
+import ntnu.systemutvikling.team6.controller.components.LoaderScene;
+import ntnu.systemutvikling.team6.controller.components.NavbarController;
+
+
+
+public class CreateUserPageController extends BaseController {
+ @FXML
+ private NavbarController navbarController;
+ @FXML
+ private FooterController footerController;
+
+ @FXML private TextField firstNameField;
+ @FXML private TextField lastNameField;
+ @FXML private TextField emailField;
+ @FXML private PasswordField passwordField;
+ @FXML private PasswordField confirmPasswordField;
+
+
+ @Override
+ protected void authTokenisSet() {
+ if (isLoggedin()){
+ LoaderScene.LoadScene("frontPage", new ActionEvent(), null, null, authToken);
+ }
+ navbarController.setAuthToken(authToken);
+ footerController.setAuthToken(authToken);
+ }
+
+ @FXML
+ private void handleCreateAccount(ActionEvent event){
+ String firstNameText = firstNameField.getText();
+ String lastNameText = lastNameField.getText();
+ String emailText = emailField.getText();
+ String password = passwordField.getText();
+ String confirmPassword = confirmPasswordField.getText();
+
+ if (firstNameText.isBlank() || lastNameText.isBlank() || emailText.isBlank() || password.isBlank() || confirmPassword.isBlank()) {
+ showAlert(Alert.AlertType.ERROR, "Empty input", "Please fill out all fields");
+ return;
+ }
+
+ if (emailText == null || emailText.isBlank() || !emailText.contains("@") || !emailText.contains(".")) {
+ showAlert(Alert.AlertType.ERROR, "Invalid Email", "Please enter a valid email");
+ return;
+ }
+
+ if (!password.equals(confirmPassword)) {
+ showAlert(Alert.AlertType.ERROR, "Mismatch of password", "Password do not match");
+ return;
+ }
+
+ // Now login
+ boolean loginSuccess;
+ try {
+ String username = firstNameText + " " + lastNameText;
+ loginSuccess = authToken.register(username,emailText, confirmPassword);
+ } catch (IllegalArgumentException e) {
+ showAlert(Alert.AlertType.ERROR, "Email already taken", "Email already taken by another user.");
+ return;
+ } catch (Exception e) {
+ e.printStackTrace();
+ showAlert(Alert.AlertType.ERROR, "Unexpected Error", "Unexpected error ocurred");
+ return;
+
+ }
+ if (loginSuccess) {
+ showAlert(
+ Alert.AlertType.INFORMATION,
+ "Sign up sucsess",
+ "You have registered a new account! Please login with same credentials");
+ LoaderScene.LoadScene("loginSite", event, null, null, authToken);
+ }
+ }
+ /**
+ * Show an JavaFx alert dialog with the specified type, title, and message.
+ *
+ * @param type
+ * @param title
+ * @param message
+ */
+ private void showAlert(Alert.AlertType type, String title, String message) {
+ Alert alert = new Alert(type);
+ alert.setTitle(title);
+ alert.setHeaderText(null);
+ alert.setContentText(message);
+ alert.showAndWait();
+ }
+ @FXML
+ private void switchToLoginPage(ActionEvent event){
+ System.out.println("Click!");
+ LoaderScene.LoadScene("loginSite", event, null, null, authToken);
+ }
+}
diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java
index 4a69ed54..b3ad1c5c 100644
--- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java
+++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/DonationPageController.java
@@ -7,7 +7,10 @@
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
+import ntnu.systemutvikling.team6.controller.components.BaseController;
+import ntnu.systemutvikling.team6.controller.components.FooterController;
import ntnu.systemutvikling.team6.controller.components.LoaderScene;
+import ntnu.systemutvikling.team6.controller.components.NavbarController;
import ntnu.systemutvikling.team6.database.DAO.DonationDAO;
import ntnu.systemutvikling.team6.database.DatabaseConnection;
import ntnu.systemutvikling.team6.models.Charity;
@@ -17,7 +20,7 @@
* This controller represents the donation page, where the user can enter a donation amount and
* confirm their donation to the charity. It also has a button to return to the front page.
*/
-public class DonationPageController {
+public class DonationPageController extends BaseController {
@FXML private Charity charity;
@FXML private TextField donatioAmount;
@@ -26,8 +29,20 @@ public class DonationPageController {
@FXML private TextField donationSearchField;
+ @FXML private NavbarController navbarController;
+ @FXML private FooterController footerController;
+
private DonationDAO donationSender = new DonationDAO(new DatabaseConnection());
+ @Override
+ protected void authTokenisSet() {
+ if (isLoggedin()){
+ LoaderScene.LoadScene("frontPage", new ActionEvent(), null, null, authToken);
+ }
+ navbarController.setAuthToken(authToken);
+ footerController.setAuthToken(authToken);
+ }
+
/**
* Initialize method for the donation page. Sets the charity name label to the name of the charity
* that is being donated to. The charity is set from the original page it was called from when the
@@ -48,7 +63,7 @@ public void setCharity(Charity charity) {
* @param event
*/
public void switchToFrontPage(ActionEvent event) {
- LoaderScene.LoadScene("FrontPage", event, null, null);
+ LoaderScene.LoadScene("FrontPage", event, null, null, authToken);
}
/**
@@ -57,7 +72,7 @@ public void switchToFrontPage(ActionEvent event) {
* @param event
*/
public void switchToCharityPage(ActionEvent event) {
- LoaderScene.LoadScene("charityPage", event, charity, null);
+ LoaderScene.LoadScene("charityPage", event, charity, null,authToken);
}
/**
@@ -108,7 +123,7 @@ public void Donate(ActionEvent event) {
"Thank you!",
"You have donated " + amount + " to " + charity.getName());
donatioAmount.clear();
- LoaderScene.LoadScene("FrontPage", event, null, null);
+ LoaderScene.LoadScene("FrontPage", event, null, null, authToken);
}
}
@@ -151,6 +166,6 @@ public void handleSearch(ActionEvent event) {
return;
}
- LoaderScene.LoadScene("availableOrganization", event, null, query);
+ LoaderScene.LoadScene("availableOrganization", event, null, query, authToken);
}
}
diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/LoginPageController.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/LoginPageController.java
new file mode 100644
index 00000000..e9ec2506
--- /dev/null
+++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/controller/LoginPageController.java
@@ -0,0 +1,34 @@
+package ntnu.systemutvikling.team6.controller;
+
+import javafx.event.ActionEvent;
+import javafx.fxml.FXML;
+import ntnu.systemutvikling.team6.controller.components.BaseController;
+import ntnu.systemutvikling.team6.controller.components.FooterController;
+import ntnu.systemutvikling.team6.controller.components.LoaderScene;
+import ntnu.systemutvikling.team6.controller.components.NavbarController;
+
+
+public class LoginPageController extends BaseController {
+ @FXML private NavbarController navbarController;
+ @FXML private FooterController footerController;
+
+ @Override
+ protected void authTokenisSet() {
+ if (isLoggedin()){
+ LoaderScene.LoadScene("frontPage", new ActionEvent(), null, null, authToken);
+ }
+ navbarController.setAuthToken(authToken);
+ footerController.setAuthToken(authToken);
+ }
+
+ @FXML
+ private void handleLogin(ActionEvent event){
+
+ }
+
+ @FXML
+ private void switchToSignupPage(ActionEvent event){
+ System.out.println("Click!");
+ LoaderScene.LoadScene("creater_user_site", event, null, null, authToken);
+ }
+}
diff --git a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java
index ec33f60b..4a65c8b2 100644
--- a/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java
+++ b/helpmehelpapplication/src/main/java/ntnu/systemutvikling/team6/service/AuthenticationService.java
@@ -66,24 +66,24 @@ public boolean login(String username, String password){
* On success, the new user is set as the current user.
*
*
- * @param displayName the display name shown on the user's profile
* @param username the unique username for the new account
* @param email the email address associated with the new account
* @param password the password for the new account
* @return {@code true} if registration was successful; {@code false} if the
* username is already taken or the database operation failed
*/
- public boolean register(String displayName, String username, String email, String password ){
+ public boolean register(String username, String email, String password ){
User newUser = new User(username, email, password, Role.NORMAL_USER, new Settings(), new Inbox());
- if(userDataReader.isEmailTaken(username)){
- return false;
+ if(userDataReader.isEmailTaken(email)){
+ throw new IllegalArgumentException("Email already taken");
}
boolean success = userDataSender.registerUser(newUser);
if (success){
- currentUser = newUser;
+ // currentUser = newUser;
+ // Proceed to login first
return true;
}
return false;
diff --git a/helpmehelpapplication/src/main/resources/fxml/creater_user_site.fxml b/helpmehelpapplication/src/main/resources/fxml/creater_user_site.fxml
index 45e9ac78..946d9792 100644
--- a/helpmehelpapplication/src/main/resources/fxml/creater_user_site.fxml
+++ b/helpmehelpapplication/src/main/resources/fxml/creater_user_site.fxml
@@ -39,97 +39,8 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
\ No newline at end of file
diff --git a/helpmehelpapplication/src/main/resources/fxml/loginSite.fxml b/helpmehelpapplication/src/main/resources/fxml/loginSite.fxml
index e3d0b571..75250c40 100644
--- a/helpmehelpapplication/src/main/resources/fxml/loginSite.fxml
+++ b/helpmehelpapplication/src/main/resources/fxml/loginSite.fxml
@@ -32,69 +32,8 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -182,6 +121,9 @@
+
+
+
@@ -198,84 +140,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
\ No newline at end of file