Skip to content

Commit b753c03

Browse files
authored
Refactor user session handling and logging
1 parent 6344564 commit b753c03

File tree

1 file changed

+6
-46
lines changed

1 file changed

+6
-46
lines changed

server.js

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function createTables() {
113113
role VARCHAR(50) DEFAULT 'employee',
114114
department VARCHAR(100),
115115
position VARCHAR(100),
116-
avatar_url TEXT DEFAULT,
116+
avatar_url TEXT,
117117
phone VARCHAR(20),
118118
is_active BOOLEAN DEFAULT 1,
119119
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
@@ -217,6 +217,7 @@ function createTables() {
217217

218218
db.run(tables[index], (err) => {
219219
if (err) {
220+
console.error('Error creating table:', err);
220221
reject(err);
221222
return;
222223
}
@@ -248,7 +249,7 @@ function createDefaultData() {
248249
passwordHash,
249250
'System Administrator',
250251
'admin',
251-
'https://i.imgur.com/image.png',
252+
'https://i.imgur.com/RpGGkQ1.png',
252253
'Management',
253254
'System Manager'
254255
],
@@ -414,11 +415,6 @@ const authenticateToken = async (req, res, next) => {
414415
try {
415416
const decoded = jwt.verify(token, JWT_SECRET);
416417

417-
const revokedToken = await dbGet('SELECT id FROM user_sessions WHERE token = ? AND is_revoked = 1', [token]);
418-
if (revokedToken) {
419-
return res.status(401).json({ error: 'Token revoked' });
420-
}
421-
422418
const user = await dbGet(
423419
`SELECT id, email, name, role, department, position, avatar_url, is_active
424420
FROM users WHERE id = ? AND is_active = 1`,
@@ -469,17 +465,6 @@ app.post('/api/auth/login', [
469465
const ipAddress = req.ip;
470466
const userAgent = req.get('User-Agent');
471467

472-
const recentFailures = await dbAll(
473-
`SELECT COUNT(*) as count FROM activity_logs
474-
WHERE ip_address = ? AND activity_type = 'login_failed'
475-
AND created_at > datetime('now', '-15 minutes')`,
476-
[ipAddress]
477-
);
478-
479-
if (recentFailures[0].count >= 5) {
480-
return res.status(429).json({ error: 'Too many failed attempts. Please try again later.' });
481-
}
482-
483468
const user = await dbGet(
484469
`SELECT id, email, password_hash, name, role, department, position, avatar_url, is_active, login_attempts, locked_until
485470
FROM users WHERE email = ?`,
@@ -525,12 +510,6 @@ app.post('/api/auth/login', [
525510

526511
const token = jwt.sign({ userId: user.id, email: user.email }, JWT_SECRET, { expiresIn: '24h' });
527512

528-
await dbRun(
529-
`INSERT INTO user_sessions (user_id, token, ip_address, user_agent, expires_at)
530-
VALUES (?, ?, ?, ?, datetime('now', '+24 hours'))`,
531-
[user.id, token, ipAddress, userAgent]
532-
);
533-
534513
await logActivity(user.id, 'login', `User ${user.name} logged in`, ipAddress, userAgent);
535514

536515
const { password_hash, login_attempts, locked_until, ...userWithoutPassword } = user;
@@ -550,11 +529,6 @@ app.post('/api/auth/login', [
550529

551530
app.post('/api/auth/logout', authenticateToken, async (req, res) => {
552531
try {
553-
const authHeader = req.headers['authorization'];
554-
const token = authHeader && authHeader.split(' ')[1];
555-
556-
await dbRun('UPDATE user_sessions SET is_revoked = 1 WHERE token = ?', [token]);
557-
558532
await logActivity(req.user.id, 'logout', `User ${req.user.name} logged out`, req.ip, req.get('User-Agent'));
559533

560534
res.json({ success: true, message: 'Logout successful' });
@@ -611,7 +585,7 @@ app.post('/api/users', authenticateToken, requireAdmin, [
611585
const result = await dbRun(
612586
`INSERT INTO users (email, password_hash, name, role, department, position, phone, avatar_url, created_by)
613587
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
614-
[email, passwordHash, name, role, department, position, phone, avatar_url || 'https://i.imgur.com/image.png', req.user.id]
588+
[email, passwordHash, name, role, department, position, phone, avatar_url || 'https://i.imgur.com/RpGGkQ1.png', req.user.id]
615589
);
616590

617591
await dbRun(`INSERT INTO user_profiles (user_id) VALUES (?)`, [result.id]);
@@ -1126,21 +1100,7 @@ app.get('/api/activities', authenticateToken, async (req, res) => {
11261100
LIMIT 200
11271101
`);
11281102

1129-
const systemLogs = [
1130-
{
1131-
id: 'system-001',
1132-
user_name: 'System',
1133-
user_role: 'system',
1134-
activity_type: 'system_audit',
1135-
description: `Activity logs accessed by ${req.user.name} (${req.user.role})`,
1136-
created_at: new Date().toISOString(),
1137-
ip_address: req.ip
1138-
}
1139-
];
1140-
1141-
const allLogs = [...systemLogs, ...activities];
1142-
1143-
res.json({ success: true, data: allLogs });
1103+
res.json({ success: true, data: activities });
11441104
} catch (error) {
11451105
console.error('Activities fetch error:', error);
11461106
res.status(500).json({ error: 'Failed to load activities' });
@@ -1336,4 +1296,4 @@ initializeDatabase().then(() => {
13361296
}).catch((error) => {
13371297
console.error('System startup error:', error);
13381298
process.exit(1);
1339-
});
1299+
});

0 commit comments

Comments
 (0)