Skip to content

Commit d7b5485

Browse files
committed
Fix organization_id handling in Clerk API calls
Address review feedback by properly handling 3 cases for organization_id: 1. Have an org id: send organization_id=THE_ORG_ID 2. Have a personal account: send organization_id= (empty string) 3. Don't know if you have an org id (old credentials): don't send organization_id param at all Changes: - Updated clerkCreateSessionToken() to check credentials.organizationId !== undefined - Updated fetchUserInfo() to handle all 3 cases consistently - Added fallback logic for old credentials without organization context - Improved logging for better debugging of organization context
1 parent 9eba5b4 commit d7b5485

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

packages/cloud/src/AuthService.ts

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,16 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
481481
const formData = new URLSearchParams()
482482
formData.append("_is_native", "1")
483483

484-
// Only add organization_id if not null (personal accounts)
484+
// Handle 3 cases for organization_id:
485+
// 1. Have an org id: organization_id=THE_ORG_ID
486+
// 2. Have a personal account: organization_id= (empty string)
487+
// 3. Don't know if you have an org id (old style credentials): don't send organization_id param at all
485488
const organizationId = this.getStoredOrganizationId()
486-
if (organizationId !== null) {
487-
formData.append("organization_id", organizationId)
489+
if (this.credentials?.organizationId !== undefined) {
490+
// We have organization context info (either org id or personal account)
491+
formData.append("organization_id", organizationId || "")
488492
}
493+
// If organizationId is undefined, don't send the param at all (old credentials)
489494

490495
const response = await fetch(`${getClerkBaseUrl()}/v1/client/sessions/${this.credentials!.sessionId}/tokens`, {
491496
method: "POST",
@@ -538,34 +543,59 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
538543

539544
userInfo.picture = userData.image_url
540545

541-
// Fetch organization info separately - but only populate if user is in organization context
546+
// Fetch organization info if user is in organization context
542547
try {
543548
const storedOrgId = this.getStoredOrganizationId()
544549

545-
if (storedOrgId !== null) {
546-
// User is in organization context - fetch user's memberships and filter
547-
const orgMemberships = await this.clerkGetOrganizationMemberships()
548-
549-
// Find the user's membership in this organization
550-
const userMembership = orgMemberships?.find(
551-
(membership: CloudOrganizationMembership) => membership.organization.id === storedOrgId,
552-
)
553-
554-
if (userMembership) {
555-
userInfo.organizationId = userMembership.organization.id
556-
userInfo.organizationName = userMembership.organization.name
557-
userInfo.organizationRole = userMembership.role
558-
userInfo.organizationImageUrl = userMembership.organization.image_url
559-
this.log("[auth] User in organization context:", {
560-
id: userMembership.organization.id,
561-
name: userMembership.organization.name,
562-
role: userMembership.role,
563-
})
550+
if (this.credentials?.organizationId !== undefined) {
551+
// We have organization context info
552+
if (storedOrgId !== null) {
553+
// User is in organization context - fetch user's memberships and filter
554+
const orgMemberships = await this.clerkGetOrganizationMemberships()
555+
556+
// Find the user's membership in this organization
557+
const userMembership = orgMemberships?.find(
558+
(membership: CloudOrganizationMembership) => membership.organization.id === storedOrgId,
559+
)
560+
561+
if (userMembership) {
562+
userInfo.organizationId = userMembership.organization.id
563+
userInfo.organizationName = userMembership.organization.name
564+
userInfo.organizationRole = userMembership.role
565+
userInfo.organizationImageUrl = userMembership.organization.image_url
566+
this.log("[auth] User in organization context:", {
567+
id: userMembership.organization.id,
568+
name: userMembership.organization.name,
569+
role: userMembership.role,
570+
})
571+
} else {
572+
this.log("[auth] Warning: User not found in stored organization:", storedOrgId)
573+
}
564574
} else {
565-
this.log("[auth] Warning: User not found in stored organization:", storedOrgId)
575+
this.log("[auth] User in personal account context - not setting organization info")
566576
}
567577
} else {
568-
this.log("[auth] User in personal account context - not setting organization info")
578+
// Old credentials without organization context - fetch organization info to determine context
579+
const orgMemberships = await this.clerkGetOrganizationMemberships()
580+
if (orgMemberships && orgMemberships.length > 0) {
581+
// Get the first (or active) organization membership
582+
const primaryOrgMembership = orgMemberships[0]
583+
const organization = primaryOrgMembership?.organization
584+
585+
if (organization) {
586+
userInfo.organizationId = organization.id
587+
userInfo.organizationName = organization.name
588+
userInfo.organizationRole = primaryOrgMembership.role
589+
userInfo.organizationImageUrl = organization.image_url
590+
this.log("[auth] Legacy credentials: Found organization membership:", {
591+
id: organization.id,
592+
name: organization.name,
593+
role: primaryOrgMembership.role,
594+
})
595+
}
596+
} else {
597+
this.log("[auth] Legacy credentials: No organization memberships found")
598+
}
569599
}
570600
} catch (error) {
571601
this.log("[auth] Failed to fetch organization info:", error)

0 commit comments

Comments
 (0)