Spaces:
Configuration error
Configuration error
File size: 4,768 Bytes
28b5ddf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class LoginPage extends JPanel {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, signupButton;
private JLabel statusLabel;
private boolean isSignUpMode = false;
private ClearPassAIGUI controller;
public LoginPage(ClearPassAIGUI controller) {
this.controller = controller;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel titleLabel = new JLabel("Welcome to ClearPass AI", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
add(titleLabel, gbc);
gbc.gridy++;
gbc.gridwidth = 1;
add(new JLabel("Username:"), gbc);
gbc.gridx = 1;
usernameField = new JTextField(15);
add(usernameField, gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
passwordField = new JPasswordField(15);
add(passwordField, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = 2;
loginButton = new JButton("Login");
signupButton = new JButton("Sign Up");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(loginButton);
buttonPanel.add(signupButton);
add(buttonPanel, gbc);
gbc.gridy++;
statusLabel = new JLabel("", SwingConstants.CENTER);
add(statusLabel, gbc);
loginButton.addActionListener(e -> {
if (isSignUpMode)
handleSignUp();
else
handleLogin();
});
signupButton.addActionListener(e -> toggleMode());
}
private void toggleMode() {
isSignUpMode = !isSignUpMode;
loginButton.setText(isSignUpMode ? "Create Account" : "Login");
signupButton.setText(isSignUpMode ? "Back to Login" : "Sign Up");
statusLabel.setText("");
}
private void handleLogin() {
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword());
if (username.isEmpty() || password.isEmpty()) {
showMessage("Please enter both fields", Color.RED);
return;
}
if (authenticate(username, password)) {
controller.setCurrentUser(username);
controller.showScreen("Welcome");
} else {
showMessage("Invalid credentials", Color.RED);
}
}
private void handleSignUp() {
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword());
if (username.isEmpty() || password.isEmpty()) {
showMessage("Please fill all fields", Color.RED);
return;
}
File file = new File("users.txt");
try {
file.createNewFile();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith(username + ":")) {
reader.close();
showMessage("Username already exists", Color.RED);
return;
}
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
writer.write(username + ":" + password);
writer.newLine();
writer.close();
JOptionPane.showMessageDialog(this, "Account created successfully!");
toggleMode();
} catch (IOException ex) {
showMessage("Error accessing file", Color.RED);
}
}
private boolean authenticate(String username, String password) {
File file = new File("users.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(":");
if (parts.length == 2 && parts[0].equals(username) && parts[1].equals(password)) {
return true;
}
}
} catch (IOException e) {
showMessage("Error reading file", Color.RED);
}
return false;
}
private void showMessage(String message, Color color) {
statusLabel.setText(message);
statusLabel.setForeground(color);
}
}
|