feat: follow lite forum updates

This commit is contained in:
lezi-fun committed 2026-10-01 14:10:12 +08:00
1 parent f59739e36f
commit b42944b687
38 files changed
+1433 -71

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import pool from '@/lib/db';
import { loadSmtpConfig, isConfigComplete, sendMail, describeSmtpError } from '@/lib/mailer';
import { normalizeEmail, saveEmailCode, invalidateEmailCode } from '@/lib/email-verification';
export async function POST(req) {
const body = await req.json().catch(() => ({}));
const email = normalizeEmail(body.email);
if (!email) return Response.json({ error: '请输入有效邮箱' }, { status: 400 });
try {
const config = await loadSmtpConfig();
if (!isConfigComplete(config)) return Response.json({ error: '站点尚未配置邮件服务' }, { status: 503 });
const user = await pool.query('SELECT id FROM users WHERE LOWER(email) = $1', [email]);
if (user.rowCount) {
const code = await saveEmailCode(email, 'reset');
if (!code) return Response.json({ error: '请 60 秒后再获取验证码' }, { status: 429 });
try {
await sendMail(config, {
to: email,
subject: 'Forumlify 密码重置验证码',
text: `你的密码重置验证码是:${code}\n\n有效期 10 分钟。如果这不是你本人的操作,请忽略本邮件。`,
});
} catch (error) {
await invalidateEmailCode(email, 'reset');
throw error;
}
}
return Response.json({ success: true, message: '如果该邮箱已注册,重置码已发送' });
} catch (error) {
return Response.json({ error: describeSmtpError(error) }, { status: 400 });
}
}
+24 -8
View File
@@ -3,20 +3,27 @@ import bcrypt from 'bcryptjs';
import pool from '@/lib/db';
import { verifyCaptcha } from '@/lib/captcha';
import { logAudit } from '@/lib/audit';
import { normalizeEmail, isEmailVerificationRequired, withEmailCode, CODE_ERRORS, ensureEmailVerificationSchema } from '@/lib/email-verification';
export async function POST(req) {
const { email, password, username, captcha_id, captcha_answer, captcha_sig } = await req.json();
if (!email || !password || !username) {
const { email, password, username, email_code, captcha_id, captcha_answer, captcha_sig } = await req.json();
const normalizedEmail = normalizeEmail(email);
if (!normalizedEmail || !password || !username) {
return Response.json({ error: '请填写完整信息' }, { status: 400 });
}
if (password.length < 6) {
return Response.json({ error: '密码至少6位' }, { status: 400 });
if (typeof password !== 'string' || password.length < 6 || password.length > 128 ||
typeof username !== 'string' || !username.trim() || username.length > 50) {
return Response.json({ error: '用户名或密码长度无效' }, { status: 400 });
}
// 服务端验证码校验(ENABLE_CAPTCHA 关闭时跳过)
if (process.env.ENABLE_CAPTCHA !== 'false' && !verifyCaptcha(captcha_id, captcha_answer, captcha_sig)) {
return Response.json({ error: '验证码错误,请重新计算' }, { status: 400 });
}
try {
const verificationRequired = await isEmailVerificationRequired();
if (verificationRequired && !/^\d{6}$/.test(email_code || '')) {
return Response.json({ error: '请输入邮箱验证码' }, { status: 400 });
}
const countResult = await pool.query('SELECT COUNT(*) FROM users');
const isFirstUser = parseInt(countResult.rows[0].count) === 0;
@@ -24,12 +31,21 @@ export async function POST(req) {
const avatar = 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username) + '&background=6366f1&color=fff&size=64';
const role = isFirstUser ? 'admin' : 'user';
const r = await pool.query(
`INSERT INTO users (email, password_hash, username, avatar_url, role)
VALUES ($1, $2, $3, $4, $5)
const insert = (client) => client.query(
`INSERT INTO users (email, password_hash, username, avatar_url, role, email_verified)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, username, avatar_url, role, created_at`,
[email, hash, username, avatar, role]
[normalizedEmail, hash, username.trim(), avatar, role, verificationRequired]
);
let r;
if (verificationRequired) {
const result = await withEmailCode(normalizedEmail, 'register', email_code, insert);
if (result.failure) return Response.json({ error: CODE_ERRORS[result.failure] }, { status: 400 });
r = result.value;
} else {
await ensureEmailVerificationSchema();
r = await insert(pool);
}
// 服务端审计:注册成功
await logAudit(req, 'register', r.rows[0].id);
@@ -0,0 +1,23 @@
import bcrypt from 'bcryptjs';
import { normalizeEmail, withEmailCode, CODE_ERRORS } from '@/lib/email-verification';
export async function POST(req) {
const body = await req.json().catch(() => ({}));
const email = normalizeEmail(body.email);
if (!email || !/^\d{6}$/.test(body.code || '') ||
typeof body.newPassword !== 'string' || body.newPassword.length < 6 || body.newPassword.length > 128) {
return Response.json({ error: '请填写有效的邮箱、验证码和新密码' }, { status: 400 });
}
try {
const hash = await bcrypt.hash(body.newPassword, 10);
const result = await withEmailCode(email, 'reset', body.code, async (client) => {
const updated = await client.query('UPDATE users SET password_hash = $1 WHERE LOWER(email) = $2 RETURNING id', [hash, email]);
if (!updated.rowCount) throw new Error('账号不存在');
return updated.rows[0];
});
if (result.failure) return Response.json({ error: CODE_ERRORS[result.failure] }, { status: 400 });
return Response.json({ success: true });
} catch {
return Response.json({ error: '重置失败,请稍后重试' }, { status: 500 });
}
}
+33
View File
@@ -0,0 +1,33 @@
import pool from '@/lib/db';
import { loadSmtpConfig, isConfigComplete, sendMail, describeSmtpError } from '@/lib/mailer';
import { normalizeEmail, isEmailVerificationRequired, saveEmailCode, invalidateEmailCode } from '@/lib/email-verification';
export async function POST(req) {
const body = await req.json().catch(() => ({}));
const email = normalizeEmail(body.email);
if (!email) return Response.json({ error: '请输入有效邮箱' }, { status: 400 });
try {
if (!await isEmailVerificationRequired()) {
return Response.json({ error: '本站未开启邮箱验证' }, { status: 400 });
}
const config = await loadSmtpConfig();
if (!isConfigComplete(config)) return Response.json({ error: '站点尚未配置邮件服务' }, { status: 503 });
const existing = await pool.query('SELECT 1 FROM users WHERE LOWER(email) = $1', [email]);
if (existing.rowCount) return Response.json({ error: '该邮箱已被注册' }, { status: 409 });
const code = await saveEmailCode(email, 'register');
if (!code) return Response.json({ error: '请 60 秒后再获取验证码' }, { status: 429 });
try {
await sendMail(config, {
to: email,
subject: 'Forumlify 注册验证码',
text: `你的注册验证码是:${code}\n\n有效期 10 分钟,请勿转发给他人。`,
});
} catch (error) {
await invalidateEmailCode(email, 'register');
throw error;
}
return Response.json({ success: true });
} catch (error) {
return Response.json({ error: describeSmtpError(error) }, { status: 400 });
}
}