Add user authentication with JWT login and registration

This commit is contained in:
2026-05-16 19:27:04 +08:00
parent 27a170ec0c
commit 99837bea9a
22 changed files with 1219 additions and 474 deletions
+18 -7
View File
@@ -145,17 +145,25 @@ router.post("/confirm", (req, res) => {
uploadPath,
} = temp;
const db = getDB();
const userId = req.userId;
const src = db
.prepare(
"INSERT INTO sources (source_type, source_name, file_name, file_hash, record_count) VALUES (?,?,?,?,?)",
"INSERT INTO sources (source_type, source_name, file_name, file_hash, record_count, user_id) VALUES (?,?,?,?,?,?)",
)
.run(sourceType, sourceName, originalname, fileHash, parsed.length);
.run(
sourceType,
sourceName,
originalname,
fileHash,
parsed.length,
userId,
);
const ins = db.prepare(`INSERT OR IGNORE INTO transactions (
transaction_code, source_id, source_type, transaction_date, transaction_time,
counterparty, description, notes, amount, direction, status, method, balance
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)`);
counterparty, description, notes, amount, direction, status, method, balance, user_id
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)`);
let inserted = 0,
skip = 0;
@@ -180,6 +188,7 @@ router.post("/confirm", (req, res) => {
tx.status || "",
tx.method || "",
tx.balance || null,
userId,
);
if (r.changes > 0) inserted++;
else skip++;
@@ -221,14 +230,16 @@ router.get("/sources", (req, res) => {
res.json(
getDB()
.prepare(
"SELECT s.*, COUNT(t.id) tx_count FROM sources s LEFT JOIN transactions t ON t.source_id=s.id GROUP BY s.id ORDER BY s.imported_at DESC",
"SELECT s.*, COUNT(t.id) tx_count FROM sources s LEFT JOIN transactions t ON t.source_id=s.id WHERE s.user_id = ? GROUP BY s.id ORDER BY s.imported_at DESC",
)
.all(),
.all(req.userId),
);
});
router.delete("/sources/:id", (req, res) => {
getDB().prepare("DELETE FROM sources WHERE id = ?").run(req.params.id);
getDB()
.prepare("DELETE FROM sources WHERE id = ? AND user_id = ?")
.run(req.params.id, req.userId);
res.json({ success: true });
});