From 410f6373d978a231ad4596e5cf6cb018ebcbd266 Mon Sep 17 00:00:00 2001 From: Hammer Date: Wed, 28 Jan 2026 18:55:04 +0000 Subject: [PATCH] Add admin password reset endpoint via BetterAuth --- apps/api/src/routes/admin.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/apps/api/src/routes/admin.ts b/apps/api/src/routes/admin.ts index f87c9dc..45a76e6 100644 --- a/apps/api/src/routes/admin.ts +++ b/apps/api/src/routes/admin.ts @@ -3,6 +3,7 @@ import { db } from '../db'; import { users, invites } from '../db/schema'; import { eq, desc } from 'drizzle-orm'; import { sendInviteEmail } from '../lib/email'; +import { auth } from '../lib/auth'; import type { User } from '../lib/auth'; import crypto from 'crypto'; @@ -82,6 +83,40 @@ export const adminRoutes = new Elysia({ prefix: '/admin' }) }), }) + // Reset user password + .post('/users/:id/reset-password', async ({ params, body, set }) => { + const targetUser = await db.query.users.findFirst({ + where: eq(users.id, params.id), + }); + + if (!targetUser) { + set.status = 404; + throw new Error('User not found'); + } + + try { + await auth.api.setPassword({ + body: { + userId: params.id, + newPassword: body.newPassword, + }, + }); + } catch (error) { + console.error('Failed to set password via auth.api:', error); + set.status = 500; + throw new Error('Failed to reset password'); + } + + return { success: true, message: 'Password reset successfully' }; + }, { + params: t.Object({ + id: t.String(), + }), + body: t.Object({ + newPassword: t.String({ minLength: 8 }), + }), + }) + // Delete user .delete('/users/:id', async ({ params, user, set }) => { // Prevent self-deletion