Add admin password reset endpoint via BetterAuth
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user