feat: 收官补齐全部未实现功能

- 账号体系:修改密码(/account + PATCH /api/auth/password)、自助删除账号
  (DELETE /api/auth/me)、订阅退订(token + /unsubscribe 页)
- 前台/SEO:RSS 订阅 /feed.xml、作品详情分享、canonical URL、
  文章 Article JSON-LD、品牌化错误页(i18n)
- 后台增强:图片管理 /admin/uploads、操作审计日志 /admin/audit(含清空)、
  联系消息已读标记、评论列表分页
- 工程:vitest 测试(密码哈希/内容校验器/种子完整性,12 例)+ pnpm test
- DB 迁移:contacts.read、subscribers.token、audit_logs(老库 ALTER 兼容)
- 密码逻辑抽为 server/utils/password.ts 供测试
This commit is contained in:
2026-08-06 17:26:26 +08:00
parent d67849a311
commit 685c7b51d1
47 changed files with 1600 additions and 72 deletions
+3 -23
View File
@@ -1,14 +1,10 @@
import { randomBytes, scrypt, timingSafeEqual } from 'node:crypto'
import { promisify } from 'node:util'
import { randomBytes } from 'node:crypto'
import { createError, getCookie, setCookie, deleteCookie } from 'h3'
import type { H3Event } from 'h3'
import { getDb } from './db'
import { hashPassword, verifyPassword } from './password'
const scryptAsync = promisify(scrypt) as (
password: string,
salt: string,
keylen: number,
) => Promise<Buffer>
export { hashPassword, verifyPassword }
export interface UserRow {
id: number
@@ -22,22 +18,6 @@ export interface UserRow {
export const SESSION_COOKIE = 'an_session'
const SESSION_TTL_SECONDS = 30 * 24 * 60 * 60 // 30 天
// ---------- 密码哈希(scrypt,盐 + 哈希以冒号分隔存储) ----------
export async function hashPassword(password: string): Promise<string> {
const salt = randomBytes(16).toString('hex')
const key = await scryptAsync(password, salt, 64)
return `${salt}:${key.toString('hex')}`
}
export async function verifyPassword(password: string, stored: string): Promise<boolean> {
const [salt, hash] = stored.split(':')
if (!salt || !hash) return false
const key = await scryptAsync(password, salt, 64)
const expected = Buffer.from(hash, 'hex')
return key.length === expected.length && timingSafeEqual(key, expected)
}
// ---------- 会话 ----------
export function createSession(userId: number): string {