Files
forumlify/app/api/users/[id]/role/route.js
T
lezi-fun 281460fb03 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
2026-08-09 17:02:11 +08:00

28 lines
890 B
JavaScript

// PUT /api/users/[id]/role (admin 改角色)
import pool from '@/lib/db';
import { getUser, requireAdmin } from '@/lib/auth';
export async function PUT(req, { params }) {
const { id } = params;
const user = getUser(req);
if (!user) {
return Response.json({ error: '请先登录' }, { status: 401 });
}
const forbidden = await requireAdmin(user);
if (forbidden) return forbidden;
const { role } = await req.json();
if (!['user', 'admin'].includes(role)) {
return Response.json({ error: '无效的角色' }, { status: 400 });
}
if (id === user.id) {
return Response.json({ error: '不能修改自己的角色' }, { status: 400 });
}
try {
await pool.query('UPDATE users SET role = $1 WHERE id = $2', [role, id]);
return Response.json({ success: true });
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });
}
}