Skip to content

Commit affcdfb

Browse files
committed
improvement(audit-log): use better-auth callback for password reset audit, remove cast
- Move password reset audit to onPasswordReset callback in auth config instead of coupling to better-auth's verification table internals - Remove ugly double-cast on workflowData.workspaceId in deployment activation
1 parent 5b88698 commit affcdfb

File tree

3 files changed

+12
-39
lines changed

3 files changed

+12
-39
lines changed

apps/sim/app/api/auth/reset-password/route.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import { db } from '@sim/db'
2-
import { user, verification } from '@sim/db/schema'
31
import { createLogger } from '@sim/logger'
4-
import { eq } from 'drizzle-orm'
52
import { type NextRequest, NextResponse } from 'next/server'
63
import { z } from 'zod'
7-
import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
84
import { auth } from '@/lib/auth'
95

106
export const dynamic = 'force-dynamic'
@@ -41,30 +37,6 @@ export async function POST(request: NextRequest) {
4137

4238
const { token, newPassword } = validationResult.data
4339

44-
// Resolve the user from the reset token before consuming it
45-
let actorId = 'unknown'
46-
let actorName: string | null = null
47-
let actorEmail: string | null = null
48-
try {
49-
const [verificationRecord] = await db
50-
.select({ value: verification.value })
51-
.from(verification)
52-
.where(eq(verification.identifier, `reset-password:${token}`))
53-
.limit(1)
54-
if (verificationRecord?.value) {
55-
actorId = verificationRecord.value
56-
const [userRecord] = await db
57-
.select({ name: user.name, email: user.email })
58-
.from(user)
59-
.where(eq(user.id, actorId))
60-
.limit(1)
61-
actorName = userRecord?.name ?? null
62-
actorEmail = userRecord?.email ?? null
63-
}
64-
} catch {
65-
logger.debug('Could not resolve user from reset token for audit')
66-
}
67-
6840
await auth.api.resetPassword({
6941
body: {
7042
newPassword,
@@ -73,16 +45,6 @@ export async function POST(request: NextRequest) {
7345
method: 'POST',
7446
})
7547

76-
recordAudit({
77-
actorId,
78-
actorName,
79-
actorEmail,
80-
action: AuditAction.PASSWORD_RESET,
81-
resourceType: AuditResourceType.PASSWORD,
82-
description: 'Password reset completed',
83-
request,
84-
})
85-
8648
return NextResponse.json({ success: true })
8749
} catch (error) {
8850
logger.error('Error during password reset:', { error })

apps/sim/app/api/workflows/[id]/deployments/[version]/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export async function PATCH(
299299
}
300300

301301
recordAudit({
302-
workspaceId: (workflowData as Record<string, unknown>)?.workspaceId as string | undefined,
302+
workspaceId: workflowData?.workspaceId,
303303
actorId: actorUserId,
304304
actorName: session?.user?.name,
305305
actorEmail: session?.user?.email,

apps/sim/lib/auth/auth.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ export const auth = betterAuth({
483483
throw new Error(`Failed to send reset password email: ${result.message}`)
484484
}
485485
},
486+
onPasswordReset: async ({ user: resetUser }) => {
487+
const { AuditAction, AuditResourceType, recordAudit } = await import('@/lib/audit/log')
488+
recordAudit({
489+
actorId: resetUser.id,
490+
actorName: resetUser.name,
491+
actorEmail: resetUser.email,
492+
action: AuditAction.PASSWORD_RESET,
493+
resourceType: AuditResourceType.PASSWORD,
494+
description: 'Password reset completed',
495+
})
496+
},
486497
},
487498
hooks: {
488499
before: createAuthMiddleware(async (ctx) => {

0 commit comments

Comments
 (0)