From 96111842f4de0dad161868cfeece38b7d65147f1 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 14:39:41 +0100 Subject: [PATCH] Added logic for checking if username / password exist --- src/main/java/LoginGUI.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 4df5634..d19db0e 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -43,12 +43,29 @@ public class LoginGUI extends JFrame implements ActionListener { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); - //Add login functionality - JOptionPane.showMessageDialog(this, "Login"); + if (authenticateUser(username, password)) { + JOptionPane.showMessageDialog(this, "Login successful!"); + // Perform actions after successful login + } else { + JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); + } } } + + // 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) && user.getPassword().equals(password)) { + return true; //Success + } + } + } + return false; // Fail + } public static void main(String[] args) {