Skip to content

fix(users): populate connected accounts and serve the spec's verbs - #73

Merged
gjtorikian merged 4 commits into
mainfrom
fix/connected-accounts
Aug 18, 2026
Merged

fix(users): populate connected accounts and serve the spec's verbs#73
gjtorikian merged 4 commits into
mainfrom
fix/connected-accounts

Conversation

@gjtorikian

Copy link
Copy Markdown
Collaborator

Scoped out of #71 as a separate resource; this is that fix. Nothing wrote to the connected_accounts collection — its only reference outside the store was the one read in user-features.ts — so GET /user_management/users/{id}/connected_accounts/{slug} answered every user, in every state, with a 404. The shape it would have returned (provider/provider_id) also wasn't the spec's ConnectedAccount, and the spec defines three more verbs on this path that the emulator didn't serve at all.

What the endpoint now is

The spec's ConnectedAccount is the Pipes resource (a data installation): {object, id, user_id, organization_id, scopes, auth_method, api_key_last_4, state, created_at, updated_at} — no provider field (the URL names it), ids prefixed data_installation_. Accounts are keyed by (user, slug, organization scope): the optional organization_id query parameter on every verb selects an org-scoped account, and an unscoped lookup never resolves a scoped one.

Two facts the emulator can be told now create accounts, and both are write paths the spec itself defines — unlike identities in #71, this resource has a write API:

  • POST (import). Takes the spec's ConnectedAccountDto (OAuth tokens, scopes, state). An omitted state is derived from the token combination the way the DTO describes: a token that still works — unexpired, or expired but refreshable — is connected; an expired access token with no refresh token is needs_reauthorization; neither state nor token is the 422 the spec reserves for an invalid combination. A duplicate for the same (user, provider, scope) answers 409.
  • A connectedAccounts seed key. References users by email — the same join key memberships use, since seeded user ids are generated — and organizations by name, both cross-validated at startup. State defaults to connected: saying a user has a connected account is saying it is connected.

PUT updates tokens, scopes, or state; without an explicit state, only an update carrying tokens re-derives it, so a scopes-only update cannot silently reconnect a needs_reauthorization account. DELETE disconnects by removing the account and its stored tokens — the spec's enum value disconnected is observable only in the event a deletion emits, never on a stored row, so a fresh import after a disconnect is a 201 rather than a 409 against a dead link. Deleting a user disconnects their accounts the same way.

Events

The spec defines pipes.connected_account.connected / reauthorization_needed / disconnected, whose payloads carry two fields the REST shape does not (provider_slug, data_integration_id). They're emitted from collection hooks — the repo's pattern, so seeded accounts fire the same events — and only on state transitions. One data_integration_ id is shared by every account of a provider slug, matching the integration/installation split the id prefixes name.

Not covered

The hand-authored /pipes/connections routes and the data_providers endpoint (which serves pipe connections where the spec describes a provider catalog) are untouched — they model an adjacent, non-spec surface and deserve their own decision.

Verification

bun test (867 pass, 18 new in src/workos/routes/connected-accounts.spec.ts, including the empty-state 404s, derivation rules, 409/422, org scoping, event payloads, and the user-delete cascade), bun run typecheck, bun run lint, bun run fmt:check.

Nothing wrote to the connected_accounts collection, so
GET /user_management/users/{id}/connected_accounts/{slug} answered every
user, in every state, with a 404 — and the shape it would have returned
(provider/provider_id) was not the spec's ConnectedAccount. The path is
also specified with three more verbs the emulator did not serve.

- GET returns the spec shape: user_id, organization_id, scopes,
  auth_method, api_key_last_4, state, and data_installation-prefixed ids.
  Accounts are keyed by (user, slug, organization scope), with
  organization_id validated when passed.
- POST imports an account from OAuth tokens; an omitted state is derived
  from the token combination, a duplicate answers 409, and neither state
  nor token answers 422.
- PUT updates tokens, scopes, or state; a scopes-only update does not
  silently reconnect a needs_reauthorization account.
- DELETE disconnects by removing the account and its stored tokens, so a
  later import is a fresh 201. Deleting a user disconnects their accounts.
- A connectedAccounts seed key references users by email and organizations
  by name, cross-validated at startup like memberships are.
- State changes emit pipes.connected_account.connected /
  reauthorization_needed / disconnected via collection hooks, so seeded
  accounts fire the same events; payloads carry the event-only
  provider_slug and data_integration_id fields.
@greptile-apps

greptile-apps Bot commented Aug 17, 2026

Copy link
Copy Markdown

Greptile Summary

The PR adds spec-compatible connected-account CRUD, seed configuration, organization scoping, response formatting, lifecycle events, and user-deletion cleanup.

  • Connected accounts can be imported, retrieved, updated, disconnected, and seeded.
  • PUT state derivation now evaluates the merged stored and submitted credentials.
  • Coverage tooling recognizes routes registered through shared path constants.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/workos/routes/connected-accounts.ts Implements scoped connected-account CRUD and correctly derives update state from merged retained and submitted credentials.
src/workos/routes/connected-accounts.spec.ts Covers seed behavior, response shape, state derivation, organization scope, conflicts, events, disconnection, and user deletion.
src/workos/index.ts Registers the route family, seeds connected accounts, and emits hook-driven lifecycle events.
src/workos/helpers.ts Adds REST and event formatters that exclude stored OAuth credentials.
src/workos/config-validator.ts Validates connected-account seed references, fields, states, and composite-key uniqueness.
src/workos/routes/users.ts Extends user deletion cleanup to remove connected accounts through the hooked collection.
scripts/gen-supported-lib.ts Extends static route discovery to resolve path constants used by route registrations.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Seed["connectedAccounts seed"] --> Store["Connected-account collection"]
  POST["POST import"] --> Store
  PUT["PUT merged credential update"] --> Store
  GET["GET account"] --> Store
  Store --> Format["Spec-compatible formatter"]
  Store --> Events["State-transition events"]
  DELETE["DELETE disconnect"] --> Store
  UserDelete["Delete user"] --> Store
Loading

Reviews (3): Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." | Re-trigger Greptile

Comment thread src/workos/routes/connected-accounts.ts Outdated
The support-matrix scanner only resolved literal and template
strings, so the connected-account verbs registered through the
shared ACCOUNT_PATH const were invisible: SUPPORTED.md read as if
this branch removed a Pipes endpoint instead of adding three, and
the drift check failed on a table that under-counted the routes.

EmulatorSeedConfig also gains the connectedAccounts key the README
already documents, so library consumers can pass the seed and the
matrix can claim it instead of reporting Pipes as API-only setup.
Deriving from the update DTO alone could not see what the account
already holds: an expired replacement token was reported as
needs_reauthorization even when a stored refresh token still
refreshes it, and an expiry-only update never re-derived at all,
leaving an unrefreshable account connected — each emitting (or
suppressing) the wrong transition event. A replacement access
token sent without an expiry deliberately drops the stored one:
that expiry described the token being replaced.
# Conflicts:
#	README.md
#	src/workos/index.ts
@gjtorikian
gjtorikian merged commit 20b4989 into main Aug 18, 2026
9 checks passed
@gjtorikian
gjtorikian deleted the fix/connected-accounts branch August 18, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant