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 0000000..acbb442 --- /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 4a69ed5..b3ad1c5 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 0000000..e9ec250 --- /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 ec33f60..4a65c8b 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 45e9ac7..946d979 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 @@