-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.java
More file actions
335 lines (294 loc) · 14.7 KB
/
Copy pathLog.java
File metadata and controls
335 lines (294 loc) · 14.7 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package forms;
import java.util.prefs.Preferences;
import DBMySQL.ConnectionProvider;
import POSmain.Landing;
import POSmain.Maintenance;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import javax.swing.Timer;
public class Log extends javax.swing.JPanel {
private Login loginFrame;
private boolean passwordVisible = false;
private ImageIcon eyeIcon = new ImageIcon("C:\\Users\\azuce\\OneDrive\\Documents\\NetBeansProjects\\POS\\Images\\eye.png");
private ImageIcon hideIcon = new ImageIcon("C:\\Users\\azuce\\OneDrive\\Documents\\NetBeansProjects\\POS\\Images\\hide.png");
private int failedAttempts = 0;
private Timer cooldownTimer;
private int cooldownTime = 30;
private POSmain.Landing landing;
public Log(Login loginFrame) {
this.loginFrame = loginFrame;
this.landing = loginFrame.landingFrame;
initComponents();
jButton2.setIcon(eyeIcon);
loadSavedUsername();
}
private void loadSavedUsername() {
Preferences prefs = Preferences.userNodeForPackage(this.getClass());
String savedUsername = prefs.get("savedUsername", "");
jTextField1.setText(savedUsername);
jCheckBox1.setSelected(!savedUsername.isEmpty());
}
private void startCooldown() {
jButton1.setEnabled(false);
cooldownTime = 30;
jButton1.setText("Retry in " + cooldownTime + "s");
cooldownTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cooldownTime--;
if (cooldownTime > 0) {
jButton1.setText("Retry in " + cooldownTime + "s");
} else {
cooldownTimer.stop();
jButton1.setEnabled(true);
jButton1.setText("Login");
failedAttempts = 0;
}
}
});
cooldownTimer.start();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jCheckBox1 = new javax.swing.JCheckBox();
jPasswordField1 = new javax.swing.JPasswordField();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
setBackground(new java.awt.Color(204, 204, 204));
setForeground(new java.awt.Color(204, 255, 255));
jLabel1.setFont(new java.awt.Font("Segoe UI Semibold", 0, 30)); // NOI18N
jLabel1.setForeground(new java.awt.Color(51, 51, 51));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Log In");
jLabel2.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jLabel2.setForeground(new java.awt.Color(51, 51, 51));
jLabel2.setText("Name*");
jTextField1.setBackground(new java.awt.Color(255, 255, 255));
jTextField1.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jTextField1.setForeground(new java.awt.Color(30, 30, 30));
jTextField1.setPreferredSize(new java.awt.Dimension(0, 50));
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jLabel3.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jLabel3.setForeground(new java.awt.Color(51, 51, 51));
jLabel3.setText("Password*");
jCheckBox1.setBackground(new java.awt.Color(204, 204, 204));
jCheckBox1.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jCheckBox1.setForeground(new java.awt.Color(51, 51, 51));
jCheckBox1.setText("Remember me");
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox1ActionPerformed(evt);
}
});
jPasswordField1.setBackground(new java.awt.Color(255, 255, 255));
jPasswordField1.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jPasswordField1.setForeground(new java.awt.Color(30, 30, 30));
jPasswordField1.setPreferredSize(new java.awt.Dimension(0, 50));
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1ActionPerformed(evt);
}
});
jLabel4.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
jLabel4.setForeground(new java.awt.Color(51, 51, 51));
jLabel4.setText("Forgot password?");
jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel4MouseClicked(evt);
}
});
jButton1.setBackground(new java.awt.Color(204, 102, 0));
jButton1.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jButton1.setForeground(new java.awt.Color(255, 255, 255));
jButton1.setText("Log In");
jButton1.setBorderPainted(false);
jButton1.setPreferredSize(new java.awt.Dimension(0, 50));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel7.setFont(new java.awt.Font("Segoe UI Semibold", 0, 18)); // NOI18N
jLabel7.setForeground(new java.awt.Color(51, 51, 51));
jLabel7.setText("Do not have an account? Register");
jButton2.setBorderPainted(false);
jButton2.setContentAreaFilled(false);
jButton2.setFocusPainted(false);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(146, 146, 146)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jCheckBox1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 160, Short.MAX_VALUE)
.addComponent(jLabel4))
.addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addContainerGap(77, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel7)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(jLabel1)
.addGap(37, 37, 37)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(jButton2)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jCheckBox1)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel7)
.addContainerGap(162, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jCheckBox1ActionPerformed
private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jPasswordField1ActionPerformed
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed
private void jLabel4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel4MouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jLabel4MouseClicked
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
String name = jTextField1.getText();
String password = new String(jPasswordField1.getPassword());
if (name.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter both name and password.");
return;
}
if (failedAttempts >= 5) {
JOptionPane.showMessageDialog(null, "Too many failed attempts. Please wait " + cooldownTime + " seconds.");
return;
}
try {
java.sql.Connection con = ConnectionProvider.getCon();
String query1 = "SELECT * FROM users WHERE name = ? AND password = ?";
PreparedStatement ps1 = con.prepareStatement(query1);
ps1.setString(1, name);
ps1.setString(2, password);
ResultSet rs1 = ps1.executeQuery();
String query2 = "SELECT * FROM createacc WHERE lastname = ? AND password = ?";
PreparedStatement ps2 = con.prepareStatement(query2);
ps2.setString(1, name);
ps2.setString(2, password);
ResultSet rs2 = ps2.executeQuery();
boolean authenticated = false;
String userRole = "user";
if (rs1.next()) {
userRole = rs1.getString("type");
authenticated = true;
} else if (rs2.next()) {
userRole = "user";
authenticated = true;
}
if (authenticated) {
failedAttempts = 0;
landing.setUserName(name);
landing.setLoggedInUserType(userRole);
loginFrame.onLoginSuccess();
if (userRole.equalsIgnoreCase("Admin")) {
System.out.println("User logged in as Admin.");
landing.enableAdminFeatures();
} else if (userRole.equalsIgnoreCase("Employee")) {
System.out.println("User logged in as Employee.");
landing.enableEmployeeFeatures();
} else {
System.out.println("User logged in with role: " + userRole);
landing.enableUserFeatures();
}
} else {
failedAttempts++;
JOptionPane.showMessageDialog(null, "Invalid name or password. Attempts: " + failedAttempts);
if (failedAttempts >= 5) {
startCooldown();
}
}
rs1.close();
ps1.close();
rs2.close();
ps2.close();
con.close();
Preferences prefs = Preferences.userNodeForPackage(this.getClass());
if (jCheckBox1.isSelected()) {
prefs.put("savedUsername", name);
} else {
prefs.remove("savedUsername");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
}
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
if (passwordVisible) {
jPasswordField1.setEchoChar('•');
jButton2.setIcon(eyeIcon);
passwordVisible = false;
} else {
jPasswordField1.setEchoChar((char) 0);
passwordVisible = true;
jButton2.setIcon(hideIcon);
}
}//GEN-LAST:event_jButton2ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel7;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
}