Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(oauth): pre-select project on consent screen via team_id hint (#66383)

Lucas Faria2e1db906d254
diff --git a/frontend/src/scenes/oauth/oauthAuthorizeLogic.ts b/frontend/src/scenes/oauth/oauthAuthorizeLogic.ts
index 8f9a1be1ba0c..6ce7ace7fb9c 100644
--- a/frontend/src/scenes/oauth/oauthAuthorizeLogic.ts
+++ b/frontend/src/scenes/oauth/oauthAuthorizeLogic.ts
@@ -122,6 +122,7 @@ export const oauthAuthorizeLogic = kea<oauthAuthorizeLogicType>([
setReadOnlyMode: (readOnly: boolean) => ({ readOnly }),
toggleDeniedScope: (scopeObject: string) => ({ scopeObject }),
setRequiredAccessLevel: (requiredAccessLevel: 'organization' | 'team' | null) => ({ requiredAccessLevel }),
+ setTeamHint: (teamId: number | null) => ({ teamId }),
setScopesWereDefaulted: (scopesWereDefaulted: boolean) => ({ scopesWereDefaulted }),
setIsMcpResource: (isMcpResource: boolean) => ({ isMcpResource }),
loadResourceScopes: (resourceUrl: string) => ({ resourceUrl }),
@@ -313,6 +314,12 @@ export const oauthAuthorizeLogic = kea<oauthAuthorizeLogicType>([
setSelectedOrganization: (_, { organizationId }) => organizationId,
},
],
+ teamHint: [
+ null as number | null,
+ {
+ setTeamHint: (_, { teamId }) => teamId,
+ },
+ ],
showCreateProject: [
false,
{
@@ -352,16 +359,27 @@ export const oauthAuthorizeLogic = kea<oauthAuthorizeLogicType>([
}
},
loadAllTeamsSuccess: () => {
+ const teams = values.sortedTeams
+ // A team_id hint from the authorize URL (e.g. the wizard's --project-id) wins:
+ // pre-select that project and its org so the user just clicks Authorize. We only
+ // honor it if the user has access to that team (it's in their loaded teams), and
+ // consume it once — so it can't override a later manual change or a project the
+ // user creates here (both also re-fire this listener).
+ if (values.teamHint && teams && values.requiredAccessLevel === 'team') {
+ const hinted = teams.find((t) => t.id === values.teamHint)
+ actions.setTeamHint(null)
+ if (hinted) {
+ actions.setSelectedOrganization(hinted.organization, hinted.id)
+ return
+ }
+ }
// After teams load, auto-select first project if org is set but no project selected
const orgId = values.selectedOrganization
const currentTeams = values.oauthAuthorization.scoped_teams
- if (orgId && (!currentTeams || currentTeams.length === 0)) {
- const teams = values.sortedTeams
- if (teams) {
- const orgTeams = teams.filter((t) => t.organization === orgId)
- if (orgTeams.length > 0) {
- actions.setOauthAuthorizationValue('scoped_teams', [orgTeams[0].id])
- }
+ if (orgId && (!currentTeams || currentTeams.length === 0) && teams) {
+ const orgTeams = teams.filter((t) => t.organization === orgId)
+ if (orgTeams.length > 0) {
+ actions.setOauthAuthorizationValue('scoped_teams', [orgTeams[0].id])
}
}
},
@@ -665,8 +683,14 @@ export const oauthAuthorizeLogic = kea<oauthAuthorizeLogicType>([
const rawRequiredAccessLevel = searchParams['required_access_level'] as 'organization' | 'project' | null
const requiredAccessLevel = rawRequiredAccessLevel === 'project' ? 'team' : rawRequiredAccessLevel
+ // Optional project to pre-select on the consent screen (e.g. the wizard's
+ // `--project-id`). Honored only when the user has access to it; otherwise ignored.
+ const teamIdParam = Number(searchParams['team_id'])
+ const teamHint = Number.isInteger(teamIdParam) && teamIdParam > 0 ? teamIdParam : null
+
actions.setScopesWereDefaulted(scopesWereDefaulted)
actions.setIsMcpResource(isMcpResource)
+ actions.setTeamHint(teamHint)
actions.setRequiredAccessLevel(requiredAccessLevel || null)
actions.loadOAuthApplication()
actions.loadAllTeams()