|
|
@ -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<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) { |
|
|
|