Skip to content

feat(oauth): honor --project-id on the OAuth login path#743

Merged
lucasheriques merged 2 commits into
mainfrom
posthog-code/wizard-honor-project-id
Jun 26, 2026
Merged

feat(oauth): honor --project-id on the OAuth login path#743
lucasheriques merged 2 commits into
mainfrom
posthog-code/wizard-honor-project-id

Conversation

@lucasheriques

@lucasheriques lucasheriques commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Problem

--project-id is already a global flag and is honored on the CI path, but on the interactive OAuth path it was silently dropped — the project came solely from tokenResponse.scoped_teams[0] (whatever the user picked on the consent screen). So a caller that knows the target project (e.g. PostHog's in-app MCP analytics onboarding) couldn't actually pin it, and if the user picked the wrong project on consent we'd silently capture into it.

Changes

In askForWizardLogin (src/utils/setup-utils.ts):

  • Accept projectId and use it as authoritative when the user granted access to it (scoped_teams includes it).
  • If --project-id was passed but the user authorized a different project, fail loudly with a clear message instead of silently using the wrong one.
  • Fall back to scoped_teams[0] when no --project-id is given (unchanged behavior).

In performOAuthFlow (src/utils/oauth.ts):

  • Forward projectId to the authorize URL as team_id, so the PostHog consent screen can pre-select that project (paired with a dotcom change that reads the hint). Harmless against an older backend that ignores the param.

Test plan

Agent-authored (PostHog Code); not manually run against a live OAuth flow. Automated checks: tsc --noEmit clean for the changed files, prettier --check passes. Suggested manual verification: run an OAuth login with --project-id=<a project you belong to> and confirm the wizard targets it; with --project-id=<a project you don't grant> confirm it errors clearly rather than proceeding.

LLM context

Authored with PostHog Code at Lucas's direction, as the wizard half of the MCP analytics "pin the project" work. Pairs with a dotcom PR that makes the OAuth consent screen pre-select the team_id hint, and the in-app install command that now emits --project-id. The --project-id flag and its plumbing into WizardSession already existed; the only gap was inside askForWizardLogin, which dropped it on the non-CI branch.

Addendum — example & expected behavior

npx -y @posthog/wizard@latest mcp-analytics --project-id=123

What's expected once the full chain is live:

  1. The wizard opens the PostHog OAuth consent screen with team_id=123 in the authorize URL.
  2. The consent screen pre-selects project 123 (via the companion dotcom change), so the user just clicks Authorize — no manual project picking.
  3. Post-OAuth, the wizard confirms the granted project matches 123 and instruments against it.
    • If the user instead authorizes a different project, the wizard errors clearly rather than silently capturing into the wrong project.
    • With no --project-id, behavior is unchanged: it uses the granted project (scoped_teams[0]).

Against an older PostHog backend (before the consent change deploys), team_id is simply ignored and the user picks the project manually — so this is safe to merge/release independently.

Companion PRs (PostHog/posthog)

On the OAuth path the project was taken solely from the consent screen
(scoped_teams[0]), silently ignoring --project-id. Now the flag is
authoritative when the user granted access to it, and we fail loudly if they
authorized a different project instead of capturing into the wrong one. Also
forward it to the authorize URL as team_id so the consent screen can
pre-select that project.

Generated-By: PostHog Code
Task-Id: db71c482-9c12-4f75-af93-fa23836280e5
@lucasheriques lucasheriques self-assigned this Jun 26, 2026
@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci error-tracking-upload-source-maps
  • /wizard-ci mcp-analytics
  • /wizard-ci misc
  • /wizard-ci revenue

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci error-tracking-upload-source-maps/android
  • /wizard-ci error-tracking-upload-source-maps/cicd-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-nested-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-github-actions-single-stage-docker-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-gitlab-node-raw
  • /wizard-ci error-tracking-upload-source-maps/cicd-monorepo-pnpm-node-react
  • /wizard-ci error-tracking-upload-source-maps/cicd-monorepo-raw-node-react
  • /wizard-ci error-tracking-upload-source-maps/cicd-ssh-vps-node-raw
  • /wizard-ci error-tracking-upload-source-maps/flutter
  • /wizard-ci error-tracking-upload-source-maps/ios
  • /wizard-ci error-tracking-upload-source-maps/next
  • /wizard-ci error-tracking-upload-source-maps/next-no-posthog
  • /wizard-ci error-tracking-upload-source-maps/node-raw
  • /wizard-ci error-tracking-upload-source-maps/node-rollup
  • /wizard-ci error-tracking-upload-source-maps/node-rollup-typescript-plugin
  • /wizard-ci error-tracking-upload-source-maps/node-webpack
  • /wizard-ci error-tracking-upload-source-maps/nuxt-3-6
  • /wizard-ci error-tracking-upload-source-maps/nuxt-4-3
  • /wizard-ci error-tracking-upload-source-maps/react-native
  • /wizard-ci error-tracking-upload-source-maps/react-vite
  • /wizard-ci error-tracking-upload-source-maps/rust
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci misc/quack-quack
  • /wizard-ci revenue/stripe

Results will be posted here when complete.

Extract the post-OAuth project selection into a pure resolveGrantedProject
helper and unit-test it: no --project-id falls back to scoped_teams[0]
(unchanged for the main integration flow and every other program),
--project-id is honored when granted, and a different grant is flagged as a
mismatch.

Generated-By: PostHog Code
Task-Id: db71c482-9c12-4f75-af93-fa23836280e5

@edwinyjlim edwinyjlim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm and tested it

@lucasheriques lucasheriques merged commit 48d8362 into main Jun 26, 2026
17 checks passed
@lucasheriques lucasheriques deleted the posthog-code/wizard-honor-project-id branch June 26, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants