Skip to content

Commit

Permalink
Feat: System can now make a user (+ Fxml for login and register and c…
Browse files Browse the repository at this point in the history
…ontroller for register). Login needs handling
  • Loading branch information
AdrianBalunan committed Apr 16, 2026
1 parent adbed95 commit 33505ee
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 332 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -151,6 +166,6 @@ public void handleSearch(ActionEvent event) {
return;
}

LoaderScene.LoadScene("availableOrganization", event, null, query);
LoaderScene.LoadScene("availableOrganization", event, null, query, authToken);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,24 @@ public boolean login(String username, String password){
* On success, the new user is set as the current user.
* </p>
*
* @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;
Expand Down
Loading

0 comments on commit 33505ee

Please sign in to comment.