feat(api): admin reset-password endpoint

This commit is contained in:
Bas van Rossem
2026-06-24 22:57:35 +02:00
parent 40977f8abd
commit 4b80898b0e
2 changed files with 88 additions and 1 deletions

View File

@@ -1,6 +1,12 @@
import { Hono } from 'hono';
import { asc, eq } from 'drizzle-orm';
import { CreateUserInput, SetRoleInput, type AdminUser, type Role } from '@solelog/shared';
import {
CreateUserInput,
SetPasswordInput,
SetRoleInput,
type AdminUser,
type Role,
} from '@solelog/shared';
import { db } from '../db/client';
import { session, user } from '../db/schema';
import { auth } from '../auth';
@@ -120,3 +126,22 @@ adminUsersRoutes.post('/api/admin/users/:id/reactivate', async (c) => {
const [updated] = await db.select().from(user).where(eq(user.id, id));
return c.json(toListItem(updated));
});
adminUsersRoutes.post('/api/admin/users/:id/password', async (c) => {
const id = c.req.param('id');
const parsed = SetPasswordInput.safeParse(await c.req.json().catch(() => null));
if (!parsed.success) return c.json({ error: 'Invalid input' }, 400);
const [target] = await db.select().from(user).where(eq(user.id, id));
if (!target) return c.json({ error: 'Gebruiker niet gevonden' }, 404);
try {
await auth.api.setUserPassword({
body: { userId: id, newPassword: parsed.data.password },
headers: c.req.raw.headers,
});
} catch {
return c.json({ error: 'Wachtwoord wijzigen mislukt.' }, 400);
}
return c.json({ success: true });
});