Skip to content

Store the organization id in credentials #5002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mrubens
Copy link
Collaborator

@mrubens mrubens commented Jun 21, 2025

Important

Add handling and storage of organization IDs in authentication process in AuthService and CloudService.

  • AuthService:
    • Add organizationId to authCredentialsSchema in AuthService.ts.
    • Modify handleCallback() to accept organizationId and store it in credentials.
    • Add getStoredOrganizationId() to retrieve stored organization ID.
    • Update clerkCreateSessionToken() to include organization_id if present.
    • Update fetchUserInfo() to log organization context if applicable.
  • CloudService:
    • Modify handleAuthCallback() to pass organizationId to AuthService.
    • Add hasStoredOrganizationId() and getStoredOrganizationId() to interface with AuthService.
  • Tests:
    • Update AuthService.spec.ts to test handling of organizationId in credentials.
    • Update CloudService.test.ts to test new methods for organization ID handling.
  • Misc:
    • Update handleUri.ts to parse and pass organizationId in auth callback.
    • Update MdmService.ts to check organization compliance using stored organization ID.

This description was created by Ellipsis for 9eba5b4. You can customize this summary. It will automatically update as commits are pushed.

@mrubens mrubens requested review from cte and jr as code owners June 21, 2025 22:21
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 21, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jun 21, 2025
@@ -454,6 +481,12 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
const formData = new URLSearchParams()
formData.append("_is_native", "1")

// Only add organization_id if not null (personal accounts)
const organizationId = this.getStoredOrganizationId()
Copy link
Collaborator

Choose a reason for hiding this comment

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

If I am reading https://clerk.com/docs/reference/frontend-api/tag/Sessions#operation/createSessionToken!path=organization_id&t=request correctly (I have not poked around and actually tested this) there are actually 3 cases to consider:

  1. Have an org id: organization_id=THE_ORG_ID.
  2. Have a personal account: organization_id=.
  3. Don't know if you have an org id (old style credentials): don't send an organization_id param at all.


if (storedOrgId !== null) {
// User is in organization context - fetch user's memberships and filter
const orgMemberships = await this.clerkGetOrganizationMemberships()
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could more simply just fetch https://clerk.com/docs/reference/frontend-api/tag/Organization#operation/getOrganization but I assume we plan to have a feature that would benefit from the role "soon enough" that it's worth continuing to fetch the memberships.

@@ -505,23 +538,37 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {

userInfo.picture = userData.image_url

// Fetch organization memberships separately
// Fetch organization info separately - but only populate if user is in organization context
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is: Fetch organization info if user is in organization context? (That is: we don't fetch if we're not in org context, which is great).

userInfo.organizationName = organization.name
userInfo.organizationRole = primaryOrgMembership.role
userInfo.organizationImageUrl = organization.image_url
const storedOrgId = this.getStoredOrganizationId()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar comment to the fetch, but for different reasons: 3 cases again:

  1. Already have an org id; makes sense to refresh org info.
  2. Definitely a personal account: obviously don't fetch anything more.
  3. Old credential / don't know yet: need to fetch organization info.

(Actually, there is another option here: we could parse the session JWT and look at the o claim and get the org id that way without another call. https://clerk.com/docs/backend-requests/resources/session-tokens#default-claims But that would mean adding all that code. (Maybe worth thinking about in the medium-long term as an alternative to fetching org memberships: I believe most of what we're likely to be interested is in there.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: Triage
Development

Successfully merging this pull request may close these issues.

3 participants