Skip to content

Commit 33e5dc4

Browse files
committed
fix(auth): delete sessions before sweeping OAuth grants on reset
1 parent 40413cb commit 33e5dc4

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

‎apps/sim/lib/auth/auth.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -989,18 +989,19 @@ export const auth = betterAuth({
989989
throw new Error(`Failed to send reset password email: ${result.message}`)
990990
}
991991
},
992-
/** Runs after `onPasswordReset`; cookie-cached sessions survive until `cookieCache.maxAge`. */
992+
/**
993+
* Re-sweeps sessions after `onPasswordReset`, catching one created while it ran (e.g. a social
994+
* sign-in). Cookie-cached sessions survive until `cookieCache.maxAge`.
995+
*/
993996
revokeSessionsOnPasswordReset: true,
997+
/**
998+
* Sessions go first: OAuth issuance references its session, so once they are deleted no new
999+
* grant can be minted behind the sweep. A failure fails the reset, and the retry from a new
1000+
* link redoes the revocation.
1001+
*/
9941002
onPasswordReset: async ({ user: resetUser }, request) => {
995-
try {
996-
await revokeUserOAuthGrants(resetUser.id)
997-
} catch (error) {
998-
// Throwing would skip the session revocation Better Auth runs after this hook.
999-
logger.error('[onPasswordReset] Failed to revoke OAuth grants', {
1000-
userId: resetUser.id,
1001-
error,
1002-
})
1003-
}
1003+
await db.delete(schema.session).where(eq(schema.session.userId, resetUser.id))
1004+
await revokeUserOAuthGrants(resetUser.id)
10041005
await recordPasswordResetAudit({ kind: 'completed', userId: resetUser.id, request })
10051006
},
10061007
},

0 commit comments

Comments
 (0)