feat: allow admins to change user passwords

This commit is contained in:
2026-06-29 18:07:24 -05:00
parent 11d8f4a456
commit 765465348e
2 changed files with 16 additions and 1 deletions
+15 -1
View File
@@ -37,7 +37,21 @@ export async function PUT(request: NextRequest) {
try {
await requireRole('admin');
const body = await request.json();
const { id, first_name, last_name, comments_disabled } = body;
const { id, first_name, last_name, comments_disabled, password } = body;
// If password provided, hash and update it too
if (password) {
const { hashPassword } = await import('@/lib/auth');
const hash = await hashPassword(password);
const { rows } = await db.query(
'UPDATE users SET first_name = $1, last_name = $2, comments_disabled = $3, password_hash = $4 WHERE id = $5 RETURNING id, username, role, first_name, last_name, comments_disabled, created_at',
[first_name || null, last_name || null, comments_disabled === true, hash, id]
);
if (rows.length === 0) {
return NextResponse.json({ error: 'User not found' }, { status: 404 });
}
return NextResponse.json({ data: rows[0] });
}
const { rows } = await db.query(
'UPDATE users SET first_name = $1, last_name = $2, comments_disabled = $3 WHERE id = $4 RETURNING id, username, role, first_name, last_name, comments_disabled, created_at',