Skip to content

fix(integrations): prevent duplicate workspace creation on oauth resubmit#786

Merged
sung17 merged 1 commit into
mainfrom
fix/oauth-duplicate-workspace-creation
Jul 21, 2026
Merged

fix(integrations): prevent duplicate workspace creation on oauth resubmit#786
sung17 merged 1 commit into
mainfrom
fix/oauth-duplicate-workspace-creation

Conversation

@sung17

@sung17 sung17 commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • The shared OAuth callback (Messenger/Instagram/TikTok/Zalo/...) creates a new workspace whenever state carries no workspaceId, with no protection against the same OAuth attempt hitting the callback twice (slow provider round-trip + an impatient re-submit). Each hit created its own "New Workspace" and consumed a "workspaces" quota unit — only the first ever got a channel attached, leaving the rest as empty, quota-consuming orphans (visibly for TikTok/Zalo via a "duplicate" error, silently for Messenger/Instagram via pending-auth cookie overwrite).
  • Add createWorkspaceForNewOauthState (apps/builder/src/lib/oauth-workspace.ts): locks and caches the created workspace by a hash of state + userId for 600s, so a repeat hit from the same user reuses the workspace instead of creating another one. The lock and the cached value use separate Redis keys — sharing one key would let the cache write clobber the lock's own release bookkeeping. Falls back to creating fresh if a cached id points at a workspace deleted in the interim.
  • Fix workspace-switcher.tsx using workspace.name (not workspace.id) as a list key — duplicate "New Workspace" names (the default name for every fresh OAuth-created workspace) were triggering React's duplicate-key warning.

Changes

  • apps/builder/src/app/integrations/[...integration]/callback.ts — delegate new-workspace creation on the shared OAuth callback to the deduping helper.
  • apps/builder/src/lib/oauth-workspace.ts (new) — lock + cache the created workspace per state + userId, with a graceful fallback if the cached workspace was deleted.
  • apps/builder/src/components/workspace-switcher.tsx — key list items by workspace.id instead of workspace.name.

Test plan

  • pnpm lint
  • pnpm --filter builder check-types
  • Manual verification: trigger the same "connect new workspace via TikTok" OAuth flow twice in a row (simulating a slow-response resubmit) and confirm only one workspace + one quota unit is created, with the second hit reusing the same workspace.
  • Manual verification: two different user accounts connecting a brand-new workspace around the same time do NOT collide onto the same workspace.

…bmit

The shared OAuth callback (Messenger/Instagram/TikTok/Zalo/...) created a
new workspace whenever `state` carried no workspaceId, with no protection
against the same OAuth attempt hitting the callback twice (slow provider
round-trip + an impatient re-submit). Each hit created its own "New
Workspace" and consumed a "workspaces" quota unit; only the first ever got
a channel attached, leaving the rest as empty, quota-consuming orphans
(loudly, via a "duplicate" error for TikTok/Zalo; silently, via cookie
overwrite, for Messenger/Instagram).

Add createWorkspaceForNewOauthState (apps/builder/src/lib/oauth-workspace.ts):
locks and caches the created workspace by a hash of `state` + `userId` for
600s, so a repeat hit from the same user reuses the workspace instead of
creating another one. The lock and the cached value use separate Redis
keys, since sharing one key lets a cache write clobber the lock's own
release bookkeeping. Falls back to creating fresh if a cached id points at
a workspace deleted in the interim.

Also fix workspace-switcher.tsx using `workspace.name` as a list key
instead of `workspace.id` — duplicate "New Workspace" names (the default
name for every fresh OAuth-created workspace) triggered React's
duplicate-key warning.
@github-actions github-actions Bot added the bug Something isn't working as expected label Jul 21, 2026
@sung17
sung17 merged commit 76346f8 into main Jul 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant