From b3e564625f62c755d7f9889eb4e0dfb569994b84 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:21:46 +0100 Subject: [PATCH 01/12] Added GUI window for user login --- src/main/java/LoginGUI.java | 29 +++++++++++++++++++++++++++++ user.json | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/LoginGUI.java diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java new file mode 100644 index 0000000..31fc2f2 --- /dev/null +++ b/src/main/java/LoginGUI.java @@ -0,0 +1,29 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; + +public class LoginGUI extends JFrame implements ActionListener { + + public LoginGUI() { + setTitle("Login"); + setSize(300, 150); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLayout(null); + + + } + + @Override + public void actionPerformed(ActionEvent e) { + + } + + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + LoginGUI loginGUI = new LoginGUI(); + loginGUI.setVisible(true); + }); + } +} \ No newline at end of file diff --git a/user.json b/user.json index 0201a67..78ef422 100644 --- a/user.json +++ b/user.json @@ -10,5 +10,23 @@ "userName": "asd1", "password": "123456", "birthday": "1" + }, + { + "id": "b54391dc-c06b-496c-828a-501f9a182cf9", + "userName": "new user", + "password": "123456", + "birthday": "1" + }, + { + "id": "6db2ea00-246d-4604-bef2-0875c3cb550e", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000" + }, + { + "id": "864695a6-8037-41e5-9f0e-4b92744b2113", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000" } ] \ No newline at end of file From 061af6313f155018174364cd7f10519aed3b09b0 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:24:42 +0100 Subject: [PATCH 02/12] Added textfield for username input --- src/main/java/LoginGUI.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 31fc2f2..b4e90a2 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -4,6 +4,7 @@ import java.awt.event.ActionListener; import java.util.List; public class LoginGUI extends JFrame implements ActionListener { + private JTextField usernameField; public LoginGUI() { setTitle("Login"); @@ -11,6 +12,13 @@ public class LoginGUI extends JFrame implements ActionListener { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); + JLabel usernameLabel = new JLabel("Username:"); + usernameLabel.setBounds(20, 20, 80, 25); + add(usernameLabel); + + usernameField = new JTextField(); + usernameField.setBounds(100, 20, 160, 25); + add(usernameField); } From fce141d40ef0cf9768ef311097c5840a5bd8c381 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:26:07 +0100 Subject: [PATCH 03/12] Added textfield for password input --- src/main/java/LoginGUI.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index b4e90a2..879cb74 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -5,6 +5,7 @@ import java.util.List; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; + private JPasswordField passwordField; public LoginGUI() { setTitle("Login"); @@ -20,6 +21,14 @@ public class LoginGUI extends JFrame implements ActionListener { usernameField.setBounds(100, 20, 160, 25); add(usernameField); + JLabel passwordLabel = new JLabel("Password:"); + passwordLabel.setBounds(20, 50, 80, 25); + add(passwordLabel); + + passwordField = new JPasswordField(); + passwordField.setBounds(100, 50, 160, 25); + add(passwordField); + } @Override From 2fe0eb289b1b0d5679f8002a1e0afa405d73c38f Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:28:15 +0100 Subject: [PATCH 04/12] Added action button for login window --- src/main/java/LoginGUI.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 879cb74..08b19a2 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -6,6 +6,7 @@ import java.util.List; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; + private JButton loginButton; public LoginGUI() { setTitle("Login"); @@ -29,6 +30,11 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.setBounds(100, 50, 160, 25); add(passwordField); + loginButton = new JButton("Login"); + loginButton.setBounds(100, 90, 100, 25); + loginButton.addActionListener(this); + add(loginButton); + } @Override From d0428d81ca59717d1e5404d6981dc18d36fbb7e0 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 11:31:51 +0100 Subject: [PATCH 05/12] Added basic click functionality to login button --- src/main/java/LoginGUI.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 08b19a2..4df5634 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -39,7 +39,15 @@ public class LoginGUI extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { - + if (e.getSource() == loginButton) { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + + //Add login functionality + JOptionPane.showMessageDialog(this, "Login"); + + + } } From 96111842f4de0dad161868cfeece38b7d65147f1 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 14:39:41 +0100 Subject: [PATCH 06/12] 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) { From 13d0a13392071df08b1e90ca0ee7b9e72afd2a86 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 14:57:22 +0100 Subject: [PATCH 07/12] Made it so pressing enter on the keyboard activates the login and ok button --- src/main/java/LoginGUI.java | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index d19db0e..e18d2ea 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -2,6 +2,8 @@ import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; @@ -35,22 +37,26 @@ public class LoginGUI extends JFrame implements ActionListener { loginButton.addActionListener(this); add(loginButton); + getRootPane().setDefaultButton(loginButton); + } @Override public void actionPerformed(ActionEvent e) { - if (e.getSource() == loginButton) { - String username = usernameField.getText(); - String password = new String(passwordField.getPassword()); + if (e.getSource() == loginButton) { + login(); + } + } - if (authenticateUser(username, password)) { + private void login() { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + + if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); // Perform actions after successful login - } else { + } else { JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); - } - - } } @@ -68,6 +74,21 @@ public class LoginGUI extends JFrame implements ActionListener { } + private class EnterKeyListener implements KeyListener { + @Override + public void keyTyped(KeyEvent e) {} + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + login(); + } + } + + @Override + public void keyReleased(KeyEvent e) {} + } + public static void main(String[] args) { SwingUtilities.invokeLater(() -> { LoginGUI loginGUI = new LoginGUI(); From 36bd9110bfe88dcc40909ccffe27ae90b17353b3 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 15:03:05 +0100 Subject: [PATCH 08/12] The login window now closes automatically after a successful login --- src/main/java/LoginGUI.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index e18d2ea..5bc3739 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -55,6 +55,8 @@ public class LoginGUI extends JFrame implements ActionListener { 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); } From 3a38f7a3235289458e75ed9ba356cec7e4532052 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 15:12:23 +0100 Subject: [PATCH 09/12] Added a checkbox to the login window to stay logged in --- src/main/java/LoginGUI.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 5bc3739..696e3a5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -9,10 +9,11 @@ public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; + private JCheckBox stayLoggedInCheckbox; public LoginGUI() { setTitle("Login"); - setSize(300, 150); + setSize(300, 180); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -32,8 +33,12 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.setBounds(100, 50, 160, 25); add(passwordField); + stayLoggedInCheckbox = new JCheckBox("Stay Logged In"); + stayLoggedInCheckbox.setBounds(20, 80, 150, 25); + add(stayLoggedInCheckbox); + loginButton = new JButton("Login"); - loginButton.setBounds(100, 90, 100, 25); + loginButton.setBounds(100, 110, 100, 25); loginButton.addActionListener(this); add(loginButton); From cec86a85d870e0a978d3ecf6007ef88625885c72 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 17:25:23 +0100 Subject: [PATCH 10/12] Added a boolean value along getter and setter methods to CreateUser that later checks if the user wants to stay logged in --- src/main/java/CreateUser.java | 9 +++++++++ src/main/java/LoginGUI.java | 8 ++++++++ user.json | 32 ++++++++------------------------ 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 8e8f15f..6bccbd9 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -17,6 +17,7 @@ class CreateUser { private String userName; private String password; private String birthday; + private boolean stayLoggedIn; // Constructor @@ -59,6 +60,14 @@ class CreateUser { public void setBirthday(String birthday) { this.birthday = birthday; } + + public boolean isStayLoggedIn() { + return stayLoggedIn; + } + + public void setStayLoggedIn(boolean stayLoggedIn) { + this.stayLoggedIn = stayLoggedIn; + } // Function to create user with validation public static CreateUser createUser(String id, String userName, String password, String birthday) { diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 696e3a5..b273014 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -44,6 +44,8 @@ public class LoginGUI extends JFrame implements ActionListener { getRootPane().setDefaultButton(loginButton); + passwordField.addKeyListener(new EnterKeyListener()); + } @Override @@ -56,6 +58,7 @@ public class LoginGUI extends JFrame implements ActionListener { 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!"); @@ -67,6 +70,11 @@ public class LoginGUI extends JFrame implements ActionListener { } } + private void updateStayLoggedIn(String username, boolean stayLoggedIn) { + // Update stayLoggedIn in the JSON file for the user + + } + // 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"); diff --git a/user.json b/user.json index 78ef422..1f77562 100644 --- a/user.json +++ b/user.json @@ -1,32 +1,16 @@ [ { - "id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", - "userName": "asd", - "password": "123456", - "birthday": "1" - }, - { - "id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", - "userName": "asd1", - "password": "123456", - "birthday": "1" - }, - { - "id": "b54391dc-c06b-496c-828a-501f9a182cf9", - "userName": "new user", - "password": "123456", - "birthday": "1" - }, - { - "id": "6db2ea00-246d-4604-bef2-0875c3cb550e", + "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", "userName": "Another Test User", "password": "TestPasswort123", - "birthday": "01.01.2000" + "birthday": "01.01.2000", + "stayLoggedIn": false }, { - "id": "864695a6-8037-41e5-9f0e-4b92744b2113", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000" + "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", + "userName": "TestUser2", + "password": "123456", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file From 95deb5c64073c9138ca9e13a664edf336af02158 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:33:44 +0100 Subject: [PATCH 11/12] Added a method that changes the stayLoggedIn variable in the JSON file --- src/main/java/CreateUser.java | 24 +++++++++++++++++++++++- user.json | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 6bccbd9..c799654 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -110,12 +110,32 @@ class CreateUser { } return userList; } + + // Function to update stayLoggedIn variable in the JSON file + public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { + List userList = readUserListFromJsonFile(filename); + + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + user.setStayLoggedIn(stayLoggedIn); + break; + } + } + + try (FileWriter fileWriter = new FileWriter(filename)) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(userList, fileWriter); + System.out.println("StayLoggedIn updated in " + filename); + } catch (IOException e) { + System.out.println("Error occurred while updating StayLoggedIn in JSON file: " + e.getMessage()); + } + } public static void main(String[] args) { try { // Example usage UUID randomUUID = UUID.randomUUID(); - CreateUser user = createUser(randomUUID.toString(), "Another Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Test User", "TestPasswort123", "01.01.2000"); // Example of accessing properties System.out.println("UserID: " + user.getId()); @@ -126,6 +146,8 @@ class CreateUser { // Save user information to a JSON file user.saveToJsonFile("user.json"); + + updateStayLoggedIn("user.json", "Test User", true); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } diff --git a/user.json b/user.json index 1f77562..893012b 100644 --- a/user.json +++ b/user.json @@ -12,5 +12,19 @@ "password": "123456", "birthday": "01.01.2000", "stayLoggedIn": false + }, + { + "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false + }, + { + "id": "503f2d6a-389c-4675-8044-3ec9ca73f1b5", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file From fe008a9ebede08ec0f8e7261a06a5aa99fedc84b Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:39:04 +0100 Subject: [PATCH 12/12] Added a method to LoginGUI that calls the updateStayLoggdIn method in CreateUser if the stay logged in checkbox is ticked --- src/main/java/LoginGUI.java | 13 +++++++++++-- user.json | 11 ++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index b273014..675ffa5 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -46,6 +46,15 @@ public class LoginGUI extends JFrame implements ActionListener { passwordField.addKeyListener(new EnterKeyListener()); + stayLoggedInCheckbox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); + String username = usernameField.getText(); + updateStayLoggedIn(username, stayLoggedIn); + } + }); + } @Override @@ -71,8 +80,8 @@ public class LoginGUI extends JFrame implements ActionListener { } private void updateStayLoggedIn(String username, boolean stayLoggedIn) { - // Update stayLoggedIn in the JSON file for the user - + // Update stayLoggedIn in the JSON file for the user + CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn); } // Function to authenticate the user by comparing the entered username and password with the saved user data diff --git a/user.json b/user.json index 893012b..5437c96 100644 --- a/user.json +++ b/user.json @@ -16,15 +16,8 @@ { "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", "userName": "Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000", - "stayLoggedIn": false - }, - { - "id": "503f2d6a-389c-4675-8044-3ec9ca73f1b5", - "userName": "Test User", - "password": "TestPasswort123", + "password": "Test", "birthday": "01.01.2000", - "stayLoggedIn": false + "stayLoggedIn": true } ] \ No newline at end of file