Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion database.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ function initDb() {
}
});

// ── Users Table (persistent auth — replaces in-memory store) ─────────────
db.run(`CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)`);

// ── Password Reset Tokens Table ───────────────────────────────────────────
db.run(`CREATE TABLE IF NOT EXISTS password_reset_tokens (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL,
token TEXT NOT NULL UNIQUE,
expires_at INTEGER NOT NULL,
created_at INTEGER DEFAULT (CAST(strftime('%s', 'now') AS INTEGER) * 1000),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)`);

// Clean up expired tokens on startup
db.run(`DELETE FROM password_reset_tokens WHERE expires_at < ${Date.now()}`);

// Pre-populate some subjects if empty
db.get('SELECT COUNT(*) as count FROM subjects', (err, row) => {
if (row && row.count === 0) {
Expand All @@ -55,4 +76,4 @@ function initDb() {
});
}

module.exports = { db, initDb };
module.exports = { db, initDb };
Loading