From d7f8db89a3df919ffbc9eb24107559b29fd91980 Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Sun, 5 Apr 2026 15:06:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9E=E6=97=B6=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/server/index.js b/server/index.js index 8da6418..a5b6ac0 100644 --- a/server/index.js +++ b/server/index.js @@ -293,7 +293,15 @@ app.post('/api/setup/admin', async (req, res) => { const hash = crypto.pbkdf2Sync(password, salt, 1000, 64, 'sha512').toString('hex'); await db.query('INSERT INTO users (username, password, salt) VALUES (?, ?, ?)', [username, hash, salt]); - res.json({ success: true, message: 'Admin account created' }); + const [userRows] = await db.query('SELECT id, username FROM users WHERE username = ?', [username]); + const user = userRows[0]; + + // Auto-login after creation so the next setup steps (like adding Prometheus) work without 401 + const sessionId = crypto.randomBytes(32).toString('hex'); + sessions.set(sessionId, { id: user.id, username: user.username }); + res.setHeader('Set-Cookie', `session_id=${sessionId}; Path=/; HttpOnly; SameSite=Strict; Max-Age=86400`); + + res.json({ success: true, message: 'Admin account created and logged in' }); } catch (err) { console.error('Admin creation error:', err); res.status(500).json({ error: err.message }); @@ -414,6 +422,9 @@ app.post('/api/sources', requireAuth, async (req, res) => { ); const [rows] = await db.query('SELECT * FROM prometheus_sources WHERE id = ?', [result.insertId]); + // Clear network history cache to force refresh + await cache.del('network_history_all'); + res.status(201).json(rows[0]); } catch (err) { console.error('Error adding source:', err); @@ -430,6 +441,9 @@ app.put('/api/sources/:id', requireAuth, async (req, res) => { 'UPDATE prometheus_sources SET name = ?, url = ?, description = ? WHERE id = ?', [name, url, description || '', req.params.id] ); + // Clear network history cache + await cache.del('network_history_all'); + const [rows] = await db.query('SELECT * FROM prometheus_sources WHERE id = ?', [req.params.id]); res.json(rows[0]); } catch (err) { @@ -442,6 +456,8 @@ app.put('/api/sources/:id', requireAuth, async (req, res) => { app.delete('/api/sources/:id', requireAuth, async (req, res) => { try { await db.query('DELETE FROM prometheus_sources WHERE id = ?', [req.params.id]); + // Clear network history cache + await cache.del('network_history_all'); res.json({ message: 'Source deleted' }); } catch (err) { console.error('Error deleting source:', err);