5c3f3bca63
- 真实后端(server/):SQLite 存储 + scrypt 会话鉴权 + SMTP 邮件 - auth 注册/登录/登出/me/资料更新 - contact 联系表单(落库+邮件)、newsletter 订阅(去重) - favorites 作品收藏、comments 新闻评论、account 管理收件箱 - 用户系统 UI:个人中心 /account(资料/收藏/评论/管理员面板)、 收藏按钮、评论组件、登录/注册弹窗接真实 API(useApiError 错误映射) - 内容单点事实源:app/data/news|work 消除三处重复,页面全部引用 - 修复断链:首页动态/作品卡可点击、分享按钮真实动作、移除 # 死链、 语言切换器、社交链接统一 siteConfig - SEO:动态 sitemap/robots、JSON-LD、脚本生成真实 og-image.png - 列表增强:搜索 + 加载更多 - 部署自动化:build-and-push.sh 支持 git tag 版本化镜像; deploy.sh 一条命令构建推送+注入 Secret/ConfigMap+kubectl 滚动升级; deployment 挂 PVC 持久化;Dockerfile 支持 better-sqlite3 原生编译 - 组件注册去目录前缀,修复 AppLoader/AnimatedButton 等未渲染问题
55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
import { readBody, createError } from 'h3'
|
|
import { getDb } from '../../utils/db'
|
|
import { hashPassword, createSession, setSessionCookie, toPublicUser, type UserRow } from '../../utils/auth'
|
|
import { isValidUsername, isValidEmail, isValidPassword, isValidPhone } from '../../utils/validation'
|
|
|
|
/**
|
|
* 注册:校验 -> scrypt 哈希 -> 落库 -> 建立会话
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody(event)
|
|
const username = String(body?.username ?? '').trim()
|
|
const email = String(body?.email ?? '').trim().toLowerCase()
|
|
const phone = String(body?.phone ?? '').trim()
|
|
const password = String(body?.password ?? '')
|
|
|
|
if (!isValidUsername(username)) {
|
|
throw createError({ statusCode: 400, message: '用户名需为 2-20 位字符', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
if (!isValidEmail(email)) {
|
|
throw createError({ statusCode: 400, message: '邮箱格式不正确', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
if (!isValidPassword(password)) {
|
|
throw createError({ statusCode: 400, message: '密码至少需要 8 位', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
if (phone && !isValidPhone(phone)) {
|
|
throw createError({ statusCode: 400, message: '手机号格式不正确', data: { code: 'VALIDATION_ERROR' } })
|
|
}
|
|
|
|
const db = getDb()
|
|
const existing = db
|
|
.prepare('SELECT username, email FROM users WHERE username = ? OR email = ?')
|
|
.get(username, email) as { username: string; email: string } | undefined
|
|
|
|
if (existing) {
|
|
if (existing.username === username) {
|
|
throw createError({ statusCode: 400, message: '用户名已被使用', data: { code: 'USERNAME_TAKEN' } })
|
|
}
|
|
throw createError({ statusCode: 400, message: '该邮箱已被注册', data: { code: 'EMAIL_TAKEN' } })
|
|
}
|
|
|
|
const passwordHash = await hashPassword(password)
|
|
const info = db
|
|
.prepare('INSERT INTO users (username, email, phone, password_hash) VALUES (?, ?, ?, ?)')
|
|
.run(username, email, phone || null, passwordHash)
|
|
|
|
const user = db
|
|
.prepare('SELECT id, username, email, phone, role, created_at FROM users WHERE id = ?')
|
|
.get(info.lastInsertRowid) as UserRow
|
|
|
|
const token = createSession(user.id)
|
|
setSessionCookie(event, token)
|
|
|
|
return { user: toPublicUser(user) }
|
|
})
|