Skip to content

Commit f61ff70

Browse files
committed
fix(oauth): evict CIMD cache on upsert failure to allow retry
1 parent 54066b9 commit f61ff70

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

apps/sim/lib/auth/auth.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import {
2525
renderPasswordResetEmail,
2626
renderWelcomeEmail,
2727
} from '@/components/emails'
28-
import { isMetadataUrl, resolveClientMetadata, upsertCimdClient } from '@/lib/auth/cimd'
28+
import {
29+
evictCachedMetadata,
30+
isMetadataUrl,
31+
resolveClientMetadata,
32+
upsertCimdClient,
33+
} from '@/lib/auth/cimd'
2934
import { sendPlanWelcomeEmail } from '@/lib/billing'
3035
import { authorizeSubscriptionReference } from '@/lib/billing/authorization'
3136
import { handleNewUser } from '@/lib/billing/core/usage'
@@ -548,7 +553,12 @@ export const auth = betterAuth({
548553
try {
549554
const { metadata, fromCache } = await resolveClientMetadata(clientId)
550555
if (!fromCache) {
551-
await upsertCimdClient(metadata)
556+
try {
557+
await upsertCimdClient(metadata)
558+
} catch (upsertErr) {
559+
evictCachedMetadata(clientId)
560+
throw upsertErr
561+
}
552562
}
553563
} catch (err) {
554564
logger.warn('CIMD resolution failed', {

apps/sim/lib/auth/cimd.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ export async function resolveClientMetadata(url: string): Promise<ResolveResult>
127127
return promise.then((doc) => ({ metadata: doc, fromCache: false }))
128128
}
129129

130+
export function evictCachedMetadata(url: string): void {
131+
cache.delete(url)
132+
}
133+
130134
export async function upsertCimdClient(metadata: ClientMetadataDocument): Promise<void> {
131135
const now = new Date()
132136
const redirectURLs = metadata.redirect_uris.join(',')

0 commit comments

Comments
 (0)