feat: port upstream custom pages, notifications, recovery codes, account security
- schema: custom_pages, notifications, recovery_codes tables + UNIQUE(conversations) + posts.edited_at - API: custom-pages CRUD (public + admin), notifications (list/read/read-all), recovery-codes (generate/count/reset-password), user avatar/password/email, post edit - UI: custom page nav links + iframe rendering, admin custom pages tab with editor modal, messages page shows notifications, settings page gains avatar upload, password/email change, recovery codes manager - Sync globals.css with upstream rewrite
This commit is contained in:
1 parent
9e72e4de8e
commit
4732e7d61d
28 files changed
+1196
-255
No files matched your search
@@ -0,0 +1,48 @@
|
||||
// PUT /api/admin/custom-pages/[id], DELETE /api/admin/custom-pages/[id]
|
||||
import pool from '@/lib/db';
|
||||
import { getUser, requireAdmin } from '@/lib/auth';
|
||||
|
||||
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export async function PUT(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
const forbidden = await requireAdmin(user);
|
||||
if (forbidden) return forbidden;
|
||||
if (!UUID_RE.test(params.id)) {
|
||||
return Response.json({ error: '页面不存在' }, { status: 404 });
|
||||
}
|
||||
|
||||
const { title, content, enabled } = await req.json();
|
||||
try {
|
||||
const r = await pool.query(
|
||||
`UPDATE custom_pages
|
||||
SET title = $1, content = $2, enabled = $3, updated_at = NOW()
|
||||
WHERE id = $4
|
||||
RETURNING *`,
|
||||
[title, content, enabled, params.id]
|
||||
);
|
||||
if (r.rows.length === 0) {
|
||||
return Response.json({ error: '页面不存在' }, { status: 404 });
|
||||
}
|
||||
return Response.json(r.rows[0]);
|
||||
} catch {
|
||||
return Response.json({ error: '更新失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
const forbidden = await requireAdmin(user);
|
||||
if (forbidden) return forbidden;
|
||||
if (!UUID_RE.test(params.id)) {
|
||||
return Response.json({ error: '页面不存在' }, { status: 404 });
|
||||
}
|
||||
try {
|
||||
await pool.query('DELETE FROM custom_pages WHERE id = $1', [params.id]);
|
||||
return Response.json({ success: true });
|
||||
} catch {
|
||||
return Response.json({ error: '删除失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// GET /api/admin/custom-pages, POST /api/admin/custom-pages — 管理 CRUD
|
||||
import pool from '@/lib/db';
|
||||
import { getUser, requireAdmin } from '@/lib/auth';
|
||||
|
||||
export async function GET(req) {
|
||||
const user = getUser(req);
|
||||
const forbidden = await requireAdmin(user);
|
||||
if (forbidden) return forbidden;
|
||||
try {
|
||||
const r = await pool.query(
|
||||
'SELECT id, name, title, content, enabled, created_at, updated_at FROM custom_pages ORDER BY created_at'
|
||||
);
|
||||
return Response.json(r.rows);
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
const forbidden = await requireAdmin(user);
|
||||
if (forbidden) return forbidden;
|
||||
|
||||
const { name, title, content } = await req.json();
|
||||
if (!name || !title || !content) {
|
||||
return Response.json({ error: '请填写完整信息' }, { status: 400 });
|
||||
}
|
||||
if (!/^[a-zA-Z0-9\-_]+$/.test(name)) {
|
||||
return Response.json({ error: '页面名称只允许字母、数字、短横线和下划线' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const r = await pool.query(
|
||||
`INSERT INTO custom_pages (name, title, content)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *`,
|
||||
[name, title, content]
|
||||
);
|
||||
return Response.json(r.rows[0]);
|
||||
} catch (err) {
|
||||
if (err.code === '23505') {
|
||||
return Response.json({ error: '页面名称已存在' }, { status: 400 });
|
||||
}
|
||||
return Response.json({ error: '创建失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// GET /api/auth/recovery-codes/count — 剩余恢复码数量
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
export async function GET(req) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
try {
|
||||
const r = await pool.query(
|
||||
'SELECT COUNT(*) FROM recovery_codes WHERE user_id = $1 AND is_used = false',
|
||||
[user.id]
|
||||
);
|
||||
return Response.json({ count: parseInt(r.rows[0].count) || 0 });
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// POST /api/auth/recovery-codes/generate — 生成 10 个恢复码
|
||||
import bcrypt from 'bcrypt';
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
function generateRecoveryCode() {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
let code = '';
|
||||
for (let i = 0; i < 20; i++) {
|
||||
code += chars[Math.floor(Math.random() * chars.length)];
|
||||
if (i === 4 || i === 9 || i === 14) code += '-';
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
export async function POST(req) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
try {
|
||||
await pool.query('DELETE FROM recovery_codes WHERE user_id = $1', [user.id]);
|
||||
const codes = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const code = generateRecoveryCode();
|
||||
codes.push(code);
|
||||
const hash = await bcrypt.hash(code, 10);
|
||||
await pool.query(
|
||||
'INSERT INTO recovery_codes (user_id, code_hash) VALUES ($1, $2)',
|
||||
[user.id, hash]
|
||||
);
|
||||
}
|
||||
return Response.json({ success: true, codes });
|
||||
} catch {
|
||||
return Response.json({ error: '生成失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// POST /api/auth/reset-password — 用恢复码重置密码(公开)
|
||||
import bcrypt from 'bcrypt';
|
||||
import pool from '@/lib/db';
|
||||
|
||||
export async function POST(req) {
|
||||
const { email, recoveryCode, newPassword } = await req.json();
|
||||
if (!email || !recoveryCode || !newPassword) {
|
||||
return Response.json({ error: '请填写完整信息' }, { status: 400 });
|
||||
}
|
||||
if (newPassword.length < 6) {
|
||||
return Response.json({ error: '密码至少6位' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const user = await pool.query('SELECT id FROM users WHERE email = $1', [email]);
|
||||
if (user.rows.length === 0) {
|
||||
return Response.json({ error: '用户不存在' }, { status: 404 });
|
||||
}
|
||||
const codes = await pool.query(
|
||||
'SELECT id, code_hash FROM recovery_codes WHERE user_id = $1 AND is_used = false',
|
||||
[user.rows[0].id]
|
||||
);
|
||||
let matched = false;
|
||||
let matchedId = null;
|
||||
for (const row of codes.rows) {
|
||||
const valid = await bcrypt.compare(recoveryCode, row.code_hash);
|
||||
if (valid) { matched = true; matchedId = row.id; break; }
|
||||
}
|
||||
if (!matched) {
|
||||
return Response.json({ error: '恢复码无效或已使用' }, { status: 400 });
|
||||
}
|
||||
await pool.query('UPDATE recovery_codes SET is_used = true WHERE id = $1', [matchedId]);
|
||||
const hash = await bcrypt.hash(newPassword, 10);
|
||||
await pool.query('UPDATE users SET password_hash = $1 WHERE id = $2', [hash, user.rows[0].id]);
|
||||
return Response.json({ success: true });
|
||||
} catch {
|
||||
return Response.json({ error: '重置失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// GET /api/custom-pages/[name] — 公开单个页面
|
||||
import pool from '@/lib/db';
|
||||
|
||||
export async function GET(req, { params }) {
|
||||
try {
|
||||
const r = await pool.query(
|
||||
'SELECT id, name, title, content FROM custom_pages WHERE name = $1 AND enabled = true',
|
||||
[params.name]
|
||||
);
|
||||
if (r.rows.length === 0) {
|
||||
return Response.json({ error: '页面不存在' }, { status: 404 });
|
||||
}
|
||||
return Response.json(r.rows[0]);
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// GET /api/custom-pages — 公开页面列表
|
||||
import pool from '@/lib/db';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const r = await pool.query(
|
||||
'SELECT id, name, title FROM custom_pages WHERE enabled = true ORDER BY created_at'
|
||||
);
|
||||
return Response.json(r.rows);
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// PUT /api/notifications/[id]/read — 标记单条已读
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export async function PUT(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
if (!UUID_RE.test(params.id)) {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
try {
|
||||
await pool.query(
|
||||
'UPDATE notifications SET is_read = true WHERE id = $1 AND user_id = $2',
|
||||
[params.id, user.id]
|
||||
);
|
||||
return Response.json({ success: true });
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// PUT /api/notifications/read-all — 全部标记已读
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
export async function PUT(req) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
try {
|
||||
await pool.query('UPDATE notifications SET is_read = true WHERE user_id = $1', [user.id]);
|
||||
return Response.json({ success: true });
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// GET /api/notifications — 我的通知列表
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
export async function GET(req) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
try {
|
||||
const r = await pool.query(
|
||||
'SELECT id, type, title, content, link, is_read, created_at FROM notifications WHERE user_id = $1 ORDER BY created_at DESC LIMIT 50',
|
||||
[user.id]
|
||||
);
|
||||
return Response.json(r.rows);
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,32 @@ export async function GET(req, { params }) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req, { params }) {
|
||||
if (!UUID_RE.test(params.id)) return invalidId();
|
||||
const user = getUser(req);
|
||||
if (!user) {
|
||||
return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
}
|
||||
const { title, content } = await req.json();
|
||||
if (!content || content.trim().length === 0) {
|
||||
return Response.json({ error: '请填写内容' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const post = await pool.query('SELECT user_id FROM posts WHERE id = $1', [params.id]);
|
||||
if (post.rows.length === 0) return invalidId();
|
||||
if (post.rows[0].user_id !== user.id) {
|
||||
return Response.json({ error: '无权限编辑此帖子' }, { status: 403 });
|
||||
}
|
||||
const r = await pool.query(
|
||||
`UPDATE posts SET title = $1, content = $2, edited_at = NOW() WHERE id = $3 RETURNING *`,
|
||||
[title || '无标题', content, params.id]
|
||||
);
|
||||
return Response.json(r.rows[0]);
|
||||
} catch {
|
||||
return Response.json({ error: '编辑失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req, { params }) {
|
||||
if (!UUID_RE.test(params.id)) return invalidId();
|
||||
const user = getUser(req);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// PUT /api/users/[id]/avatar — 修改头像
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
export async function PUT(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
if (params.id !== user.id) {
|
||||
return Response.json({ error: '无权限修改他人头像' }, { status: 403 });
|
||||
}
|
||||
const { avatar_url } = await req.json();
|
||||
if (!avatar_url) {
|
||||
return Response.json({ error: '请提供头像地址' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
await pool.query('UPDATE users SET avatar_url = $1 WHERE id = $2', [avatar_url, params.id]);
|
||||
return Response.json({ success: true, avatar_url });
|
||||
} catch {
|
||||
return Response.json({ error: '更新失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// PUT /api/users/[id]/email — 修改邮箱(需密码验证)
|
||||
import bcrypt from 'bcrypt';
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
export async function PUT(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
if (params.id !== user.id) {
|
||||
return Response.json({ error: '无权限' }, { status: 403 });
|
||||
}
|
||||
const { password, newEmail } = await req.json();
|
||||
if (!password || !newEmail) {
|
||||
return Response.json({ error: '请填写完整信息' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const u = await pool.query('SELECT password_hash FROM users WHERE id = $1', [params.id]);
|
||||
if (u.rows.length === 0) {
|
||||
return Response.json({ error: '用户不存在' }, { status: 404 });
|
||||
}
|
||||
const valid = await bcrypt.compare(password, u.rows[0].password_hash);
|
||||
if (!valid) {
|
||||
return Response.json({ error: '密码错误' }, { status: 400 });
|
||||
}
|
||||
const existing = await pool.query('SELECT id FROM users WHERE email = $1 AND id != $2', [newEmail, params.id]);
|
||||
if (existing.rows.length > 0) {
|
||||
return Response.json({ error: '邮箱已被占用' }, { status: 400 });
|
||||
}
|
||||
await pool.query('UPDATE users SET email = $1 WHERE id = $2', [newEmail, params.id]);
|
||||
return Response.json({ success: true });
|
||||
} catch {
|
||||
return Response.json({ error: '修改失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// PUT /api/users/[id]/password — 修改密码
|
||||
import bcrypt from 'bcrypt';
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
|
||||
export async function PUT(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
if (params.id !== user.id) {
|
||||
return Response.json({ error: '无权限' }, { status: 403 });
|
||||
}
|
||||
const { oldPassword, newPassword } = await req.json();
|
||||
if (!oldPassword || !newPassword) {
|
||||
return Response.json({ error: '请填写完整信息' }, { status: 400 });
|
||||
}
|
||||
if (newPassword.length < 6) {
|
||||
return Response.json({ error: '新密码至少6位' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const u = await pool.query('SELECT password_hash FROM users WHERE id = $1', [params.id]);
|
||||
if (u.rows.length === 0) {
|
||||
return Response.json({ error: '用户不存在' }, { status: 404 });
|
||||
}
|
||||
const valid = await bcrypt.compare(oldPassword, u.rows[0].password_hash);
|
||||
if (!valid) {
|
||||
return Response.json({ error: '当前密码错误' }, { status: 400 });
|
||||
}
|
||||
const hash = await bcrypt.hash(newPassword, 10);
|
||||
await pool.query('UPDATE users SET password_hash = $1 WHERE id = $2', [hash, params.id]);
|
||||
return Response.json({ success: true });
|
||||
} catch {
|
||||
return Response.json({ error: '修改失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user