feat: migrate to Next.js 14 (App Router + Route Handlers)
- Replace Express server with Next.js Route Handlers (16 API routes) - Rewrite index.html SPA as React components (feed, post, admin, modals) - Move style.css to app/globals.css with dark/light themes intact - Add lib/db.js, lib/auth.js (JWT), lib/api.js (client API wrapper) - Server binds 0.0.0.0 by default; host/port configurable via config.js - Uploads served via /api/uploads route (rewrite keeps /uploads/ URLs) - Multi-stage dockerfile using standalone output
This commit is contained in:
1 parent
ef1318da66
commit
281460fb03
52 files changed
+2833
-3372
No files matched your search
@@ -0,0 +1,40 @@
|
||||
// POST /api/auth/login
|
||||
import bcrypt from 'bcrypt';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import pool from '@/lib/db';
|
||||
import { JWT_SECRET } from '@/lib/auth';
|
||||
|
||||
export async function POST(req) {
|
||||
const { email, password } = await req.json();
|
||||
if (!email || !password) {
|
||||
return Response.json({ error: '请填写邮箱和密码' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const r = await pool.query('SELECT * FROM users WHERE email = $1', [email]);
|
||||
const user = r.rows[0];
|
||||
if (!user) {
|
||||
return Response.json({ error: '邮箱或密码错误' }, { status: 401 });
|
||||
}
|
||||
const valid = await bcrypt.compare(password, user.password_hash);
|
||||
if (!valid) {
|
||||
return Response.json({ error: '邮箱或密码错误' }, { status: 401 });
|
||||
}
|
||||
const token = jwt.sign(
|
||||
{ id: user.id, email: user.email, role: user.role },
|
||||
JWT_SECRET,
|
||||
{ expiresIn: '7d' }
|
||||
);
|
||||
return Response.json({
|
||||
token,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
avatar_url: user.avatar_url,
|
||||
bio: user.bio,
|
||||
role: user.role,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
return Response.json({ error: '登录失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// GET /api/auth/me
|
||||
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, username, avatar_url, bio, role, created_at FROM users WHERE id = $1',
|
||||
[user.id]
|
||||
);
|
||||
if (!r.rows[0]) {
|
||||
return Response.json({ error: '用户不存在' }, { status: 404 });
|
||||
}
|
||||
return Response.json(r.rows[0]);
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// POST /api/auth/register
|
||||
import bcrypt from 'bcrypt';
|
||||
import pool from '@/lib/db';
|
||||
|
||||
export async function POST(req) {
|
||||
const { email, password, username } = await req.json();
|
||||
if (!email || !password || !username) {
|
||||
return Response.json({ error: '请填写完整信息' }, { status: 400 });
|
||||
}
|
||||
if (password.length < 6) {
|
||||
return Response.json({ error: '密码至少6位' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const countResult = await pool.query('SELECT COUNT(*) FROM users');
|
||||
const isFirstUser = parseInt(countResult.rows[0].count) === 0;
|
||||
|
||||
const hash = await bcrypt.hash(password, 10);
|
||||
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)
|
||||
RETURNING id, username, avatar_url, role, created_at`,
|
||||
[email, hash, username, avatar, role]
|
||||
);
|
||||
|
||||
return Response.json({
|
||||
user: r.rows[0],
|
||||
message: isFirstUser ? '🎉 你是第一个用户,已自动设为管理员!' : '注册成功',
|
||||
});
|
||||
} catch (err) {
|
||||
if (err.code === '23505') {
|
||||
return Response.json({ error: '邮箱或用户名已被注册' }, { status: 400 });
|
||||
}
|
||||
return Response.json({ error: '注册失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user