Skip to content

Commit befc1ca

Browse files
committed
address comments
1 parent 76d984d commit befc1ca

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

apps/sim/app/workspace/[workspaceId]/knowledge/[id]/components/add-connector-modal/add-connector-modal.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
Tooltip,
2121
} from '@/components/emcn'
2222
import { useSession } from '@/lib/auth/auth-client'
23-
import { writeOAuthReturnContext } from '@/lib/credentials/client-state'
23+
import { consumeOAuthReturnContext, writeOAuthReturnContext } from '@/lib/credentials/client-state'
2424
import {
2525
getCanonicalScopesForProvider,
2626
getProviderIdFromServiceId,
@@ -593,7 +593,10 @@ export function AddConnectorModal({ open, onOpenChange, knowledgeBaseId }: AddCo
593593
{connectorConfig && connectorConfig.auth.mode === 'oauth' && connectorProviderId && (
594594
<OAuthRequiredModal
595595
isOpen={showOAuthModal}
596-
onClose={() => setShowOAuthModal(false)}
596+
onClose={() => {
597+
consumeOAuthReturnContext()
598+
setShowOAuthModal(false)
599+
}}
597600
provider={connectorProviderId}
598601
toolName={connectorConfig.name}
599602
requiredScopes={getCanonicalScopesForProvider(connectorProviderId)}

apps/sim/app/workspace/[workspaceId]/knowledge/[id]/components/connectors-section/connectors-section.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
Tooltip,
2828
} from '@/components/emcn'
2929
import { cn } from '@/lib/core/utils/cn'
30-
import { writeOAuthReturnContext } from '@/lib/credentials/client-state'
30+
import { consumeOAuthReturnContext, writeOAuthReturnContext } from '@/lib/credentials/client-state'
3131
import {
3232
getCanonicalScopesForProvider,
3333
getProviderIdFromServiceId,
@@ -475,7 +475,10 @@ function ConnectorCard({
475475
{showOAuthModal && serviceId && providerId && (
476476
<OAuthRequiredModal
477477
isOpen={showOAuthModal}
478-
onClose={() => setShowOAuthModal(false)}
478+
onClose={() => {
479+
consumeOAuthReturnContext()
480+
setShowOAuthModal(false)
481+
}}
479482
provider={providerId as OAuthProvider}
480483
toolName={connectorDef?.name ?? connector.connectorType}
481484
requiredScopes={getCanonicalScopesForProvider(providerId)}

apps/sim/app/workspace/[workspaceId]/settings/components/integrations/integrations-manager.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ export function IntegrationsManager() {
408408
}),
409409
})
410410

411-
const oauthPreCount = credentials.filter((c) => c.type === 'oauth').length
411+
const oauthPreCount = credentials.filter(
412+
(c) => c.type === 'oauth' && c.providerId === selectedOAuthService.providerId
413+
).length
412414
const returnOrigin = pendingReturnOriginRef.current
413415
pendingReturnOriginRef.current = undefined
414416

@@ -548,7 +550,9 @@ export function IntegrationsManager() {
548550
}),
549551
})
550552

551-
const oauthPreCount = credentials.filter((c) => c.type === 'oauth').length
553+
const oauthPreCount = credentials.filter(
554+
(c) => c.type === 'oauth' && c.providerId === selectedCredential.providerId
555+
).length
552556
writeOAuthReturnContext({
553557
origin: 'integrations',
554558
displayName: selectedCredential.displayName,

apps/sim/hooks/use-oauth-return.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useEffect } from 'react'
3+
import { useEffect, useRef } from 'react'
44
import { useParams, useRouter } from 'next/navigation'
55
import { toast } from '@/components/emcn'
66
import {
@@ -12,6 +12,7 @@ import { useNotificationStore } from '@/stores/notifications/store'
1212

1313
const OAUTH_CREDENTIAL_UPDATED_EVENT = 'oauth-credentials-updated'
1414
const SETTINGS_RETURN_URL_KEY = 'settings-return-url'
15+
const CONTEXT_MAX_AGE_MS = 15 * 60 * 1000
1516

1617
async function resolveOAuthMessage(ctx: OAuthReturnContext): Promise<string> {
1718
if (ctx.reconnect) {
@@ -28,11 +29,12 @@ async function resolveOAuthMessage(ctx: OAuthReturnContext): Promise<string> {
2829
providerId: string | null
2930
}>
3031

31-
if (oauthCredentials.length > ctx.preCount) {
32+
const forProvider = oauthCredentials.filter((c) => c.providerId === ctx.providerId)
33+
if (forProvider.length > ctx.preCount) {
3234
return `"${ctx.displayName}" credential connected successfully.`
3335
}
3436

35-
const existing = oauthCredentials.find((c) => c.providerId === ctx.providerId)
37+
const existing = forProvider[0]
3638
return `This account is already connected as "${existing?.displayName || ctx.displayName}".`
3739
} catch {
3840
return `"${ctx.displayName}" credential connected successfully.`
@@ -61,15 +63,19 @@ export function useOAuthReturnRouter() {
6163
const router = useRouter()
6264
const params = useParams()
6365
const workspaceId = params.workspaceId as string
66+
const handledRef = useRef(false)
6467

6568
useEffect(() => {
69+
if (handledRef.current) return
6670
const ctx = readOAuthReturnContext()
6771
if (!ctx) return
68-
if (Date.now() - ctx.requestedAt > 15 * 60 * 1000) {
72+
if (Date.now() - ctx.requestedAt > CONTEXT_MAX_AGE_MS) {
6973
consumeOAuthReturnContext()
7074
return
7175
}
7276

77+
handledRef.current = true
78+
7379
if (ctx.origin === 'integrations') {
7480
consumeOAuthReturnContext()
7581
void (async () => {
@@ -110,6 +116,7 @@ export function useOAuthReturnForWorkflow(workflowId: string) {
110116
if (!ctx || ctx.origin !== 'workflow') return
111117
if (ctx.workflowId !== workflowId) return
112118
consumeOAuthReturnContext()
119+
if (Date.now() - ctx.requestedAt > CONTEXT_MAX_AGE_MS) return
113120

114121
void (async () => {
115122
const message = await resolveOAuthMessage(ctx)
@@ -129,6 +136,7 @@ export function useOAuthReturnForKBConnectors(knowledgeBaseId: string) {
129136
if (!ctx || ctx.origin !== 'kb-connectors') return
130137
if (ctx.knowledgeBaseId !== knowledgeBaseId) return
131138
consumeOAuthReturnContext()
139+
if (Date.now() - ctx.requestedAt > CONTEXT_MAX_AGE_MS) return
132140

133141
void (async () => {
134142
const message = await resolveOAuthMessage(ctx)

0 commit comments

Comments
 (0)