Add user authentication with JWT login and registration
This commit is contained in:
+99
-34
@@ -22,6 +22,15 @@ function initDB() {
|
||||
const database = getDB();
|
||||
|
||||
database.exec(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password TEXT NOT NULL,
|
||||
nickname TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sources (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
source_type TEXT NOT NULL,
|
||||
@@ -102,6 +111,31 @@ function initDB() {
|
||||
CREATE INDEX IF NOT EXISTS idx_dt_deal ON deal_transactions(deal_id);
|
||||
`);
|
||||
|
||||
// 兼容旧库:为所有业务表添加 user_id 字段
|
||||
const tables = ["sources", "transactions", "deals", "categories"];
|
||||
for (const table of tables) {
|
||||
try {
|
||||
const cols = database
|
||||
.prepare("PRAGMA table_info(" + table + ")")
|
||||
.all()
|
||||
.map((r) => r.name);
|
||||
if (!cols.includes("user_id")) {
|
||||
database.exec(
|
||||
"ALTER TABLE " +
|
||||
table +
|
||||
" ADD COLUMN user_id INTEGER REFERENCES users(id)",
|
||||
);
|
||||
database.exec(
|
||||
"CREATE INDEX IF NOT EXISTS idx_" +
|
||||
table +
|
||||
"_user ON " +
|
||||
table +
|
||||
"(user_id)",
|
||||
);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
// 兼容旧库:为 deals 表添加新字段
|
||||
try {
|
||||
const dealCols = database
|
||||
@@ -111,9 +145,11 @@ function initDB() {
|
||||
const newCols = [
|
||||
["direction", "TEXT"],
|
||||
["category", "TEXT"],
|
||||
["ai_category", "TEXT"],
|
||||
["ai_description", "TEXT"],
|
||||
["ai_confidence", "REAL"],
|
||||
["reviewed", "INTEGER DEFAULT 0"],
|
||||
["review_notes", "TEXT"],
|
||||
["reviewed_at", "DATETIME"],
|
||||
["tags", "TEXT"],
|
||||
["updated_at", "DATETIME"],
|
||||
@@ -124,42 +160,71 @@ function initDB() {
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
// 仅首次初始化默认分类
|
||||
const catCount = database
|
||||
.prepare("SELECT COUNT(*) as c FROM categories")
|
||||
.get().c;
|
||||
if (catCount === 0) {
|
||||
const ins = database.prepare(
|
||||
"INSERT INTO categories (name, keywords) VALUES (?, ?)",
|
||||
);
|
||||
const defaults = [
|
||||
["餐饮美食", '["餐厅","外卖","食堂","咖啡","奶茶","零食","水果","买菜"]'],
|
||||
[
|
||||
"交通出行",
|
||||
'["公交","地铁","打车","出租车","加油","停车","高速","火车","飞机","骑行"]',
|
||||
],
|
||||
[
|
||||
"购物消费",
|
||||
'["淘宝","京东","拼多多","超市","百货","服饰","数码","家电","日用品"]',
|
||||
],
|
||||
["住房相关", '["房租","房贷","物业","水电","燃气","维修","装修"]'],
|
||||
["通讯网络", '["话费","宽带","流量"]'],
|
||||
["投资理财", '["基金","股票","理财","定期","转入","转出","收益"]'],
|
||||
["工资收入", '["工资","薪资","奖金","报销","劳务"]'],
|
||||
["转账红包", '["转账","红包","汇款","收款","提现","充值"]'],
|
||||
["医疗健康", '["医院","药店","挂号","体检","保险"]'],
|
||||
["教育学习", '["学费","培训","书本","课程","考试"]'],
|
||||
["娱乐休闲", '["电影","演出","旅游","酒店","游戏","充值","会员"]'],
|
||||
["信用卡还款", '["还款","信用卡","花呗","借呗","白条"]'],
|
||||
["其他杂项", "[]"],
|
||||
];
|
||||
database.transaction(() => {
|
||||
defaults.forEach((c) => ins.run(c[0], c[1]));
|
||||
})();
|
||||
console.log("[Reco] 已初始化默认分类");
|
||||
// 兼容旧库:config 表 key PRIMARY KEY -> id + UNIQUE(key,user_id)
|
||||
try {
|
||||
const cfgCols = database
|
||||
.prepare("PRAGMA table_info(config)")
|
||||
.all()
|
||||
.map((r) => r.name);
|
||||
if (!cfgCols.includes("id")) {
|
||||
database.exec(`
|
||||
CREATE TABLE IF NOT EXISTS config_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
key TEXT NOT NULL,
|
||||
value TEXT,
|
||||
user_id INTEGER REFERENCES users(id),
|
||||
UNIQUE(key, user_id)
|
||||
);
|
||||
INSERT OR IGNORE INTO config_new (key, value, user_id)
|
||||
SELECT key, value, NULL FROM config;
|
||||
DROP TABLE config;
|
||||
ALTER TABLE config_new RENAME TO config;
|
||||
`);
|
||||
console.log("[Reco] config 表已迁移为 id+UNIQUE(key,user_id)");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("[Reco] config 迁移失败:", e.message);
|
||||
}
|
||||
|
||||
console.log("[Reco] 数据库初始化完成");
|
||||
}
|
||||
|
||||
module.exports = { getDB, initDB };
|
||||
// 为新用户创建默认分类
|
||||
function initDefaultCategories(userId) {
|
||||
const database = getDB();
|
||||
const existing = database
|
||||
.prepare("SELECT COUNT(*) as c FROM categories WHERE user_id = ?")
|
||||
.get(userId).c;
|
||||
if (existing > 0) return;
|
||||
|
||||
const ins = database.prepare(
|
||||
"INSERT INTO categories (name, keywords, user_id) VALUES (?, ?, ?)",
|
||||
);
|
||||
const defaults = [
|
||||
["餐饮美食", '["餐厅","外卖","食堂","咖啡","奶茶","零食","水果","买菜"]'],
|
||||
[
|
||||
"交通出行",
|
||||
'["公交","地铁","打车","出租车","加油","停车","高速","火车","飞机","骑行"]',
|
||||
],
|
||||
[
|
||||
"购物消费",
|
||||
'["淘宝","京东","拼多多","超市","百货","服饰","数码","家电","日用品"]',
|
||||
],
|
||||
["住房相关", '["房租","房贷","物业","水电","燃气","维修","装修"]'],
|
||||
["通讯网络", '["话费","宽带","流量"]'],
|
||||
["投资理财", '["基金","股票","理财","定期","转入","转出","收益"]'],
|
||||
["工资收入", '["工资","薪资","奖金","报销","劳务"]'],
|
||||
["转账红包", '["转账","红包","汇款","收款","提现","充值"]'],
|
||||
["医疗健康", '["医院","药店","挂号","体检","保险"]'],
|
||||
["教育学习", '["学费","培训","书本","课程","考试"]'],
|
||||
["娱乐休闲", '["电影","演出","旅游","酒店","游戏","充值","会员"]'],
|
||||
["信用卡还款", '["还款","信用卡","花呗","借呗","白条"]'],
|
||||
["其他杂项", "[]"],
|
||||
];
|
||||
database.transaction(() => {
|
||||
defaults.forEach((c) => ins.run(c[0], c[1], userId));
|
||||
})();
|
||||
console.log(`[Reco] 已为用户 ${userId} 初始化默认分类`);
|
||||
}
|
||||
|
||||
module.exports = { getDB, initDB, initDefaultCategories };
|
||||
|
||||
Reference in New Issue
Block a user