From bb429f6f1dc1853865a771c966c78166f0cdb26c Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Fri, 9 Feb 2024 19:52:07 +0100 Subject: [PATCH] refactoring: fixed indentations and removed useless comments in LoginGUI --- src/main/java/CreateUser.java | 10 +++--- src/main/java/LoginGUI.java | 57 +++++++++++++++-------------------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 7155014..5a84cf6 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -105,7 +105,7 @@ class CreateUser { } return new CreateUser(id, userName, password, birthday, firstName, surname); } - // Function to hash the password using SHA-256 algorithm + // Function to hash the password using SHA-256 algorithm private String hashPassword(String password) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); @@ -123,7 +123,7 @@ class CreateUser { } } - // Function to save to JSON file, replace with database call later + // Function to save to JSON file, replace with database call later public void saveToJsonFile(String filename) { List userList = readUserListFromJsonFile(filename); userList.add(this); @@ -133,11 +133,11 @@ class CreateUser { gson.toJson(userList, fileWriter); System.out.println("User information appended to " + filename); } catch (IOException e) { - System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage()); + System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage()); } } - // Function to read user information from a JSON file + // Function to read user information from a JSON file public static List readUserListFromJsonFile(String filename) { List userList = new ArrayList<>(); try (Reader reader = new FileReader(filename)) { @@ -152,7 +152,7 @@ class CreateUser { return userList; } - // Function to update stayLoggedIn variable in the JSON file + // Function to update stayLoggedIn variable in the JSON file public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { List userList = readUserListFromJsonFile(filename); diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 6d49398..6731ae7 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -35,7 +35,6 @@ public class LoginGUI extends JFrame implements ActionListener { return signUpButton; } - public LoginGUI() { setTitle("Login"); setSize(300, 220); @@ -81,9 +80,9 @@ public class LoginGUI extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); String username = usernameField.getText(); - // Set stayLoggedIn to false if the checkbox is unchecked + // Set stayLoggedIn to false if the checkbox is unchecked if (!stayLoggedInCheckbox.isSelected()) { - stayLoggedIn = false; + stayLoggedIn = false; } updateStayLoggedIn(username, stayLoggedIn); } @@ -94,63 +93,57 @@ public class LoginGUI extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { - login(); + login(); }else if (e.getSource() == signUpButton) { SignUpGUI signUpGUI = new SignUpGUI(); signUpGUI.setVisible(true); - } } private void login() { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); - boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); // Get checkbox state if (authenticateUser(username, password)) { - JOptionPane.showMessageDialog(this, "Login successful!"); - // Perform actions after successful login - + JOptionPane.showMessageDialog(this, "Login successful!"); dispose(); } else { JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); } } - private void updateStayLoggedIn(String username, boolean stayLoggedIn) { // Update stayLoggedIn in the JSON file for the user - CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn); + private void updateStayLoggedIn(String username, boolean stayLoggedIn) { + CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn); } - // Function to authenticate the user by comparing the entered username and password with the saved user data + // Function to authenticate the user by comparing the entered username and password with the saved user data private boolean authenticateUser(String username, String password) { List userList = CreateUser.readUserListFromJsonFile("user.json"); if (userList != null) { for (CreateUser user : userList) { if (user.getUserName().equals(username)) { - // Hash the user input password - String hashedPassword = hashPassword(password); - // Compare the hashed passwords - if (user.getPassword().equals(hashedPassword)) { - return true; // Success - } - } - } + String hashedPassword = hashPassword(password); + if (user.getPassword().equals(hashedPassword)) { + return true; + } + } + } } - return false; // Fail + return false; } - private String hashPassword(String password) { - try { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] hash = digest.digest(password.getBytes()); - StringBuilder hexString = new StringBuilder(); - for (byte b : hash) { - String hex = Integer.toHexString(0xff & b); - if (hex.length() == 1) { - hexString.append('0'); - } - hexString.append(hex); + private String hashPassword(String password) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(password.getBytes()); + StringBuilder hexString = new StringBuilder(); + for (byte b : hash) { + String hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) { + hexString.append('0'); + } + hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) {