Browse Source

refactoring: fixed indentations and removed useless comments in LoginGUI

remotes/origin/hotfix/client/improve-chat-window-appearence
Richard Schmidt 11 months ago
parent
commit
bb429f6f1d
  1. 10
      src/main/java/CreateUser.java
  2. 55
      src/main/java/LoginGUI.java

10
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<CreateUser> 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<CreateUser> readUserListFromJsonFile(String filename) {
List<CreateUser> 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<CreateUser> userList = readUserListFromJsonFile(filename);

55
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
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<CreateUser> 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) {

Loading…
Cancel
Save