@@ -552,17 +552,10 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
552552 if ( storedOrgId !== null ) {
553553 // User is in organization context - fetch user's memberships and filter
554554 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- )
555+ const userMembership = this . findOrganizationMembership ( orgMemberships , storedOrgId )
560556
561557 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
558+ this . setUserOrganizationInfo ( userInfo , userMembership )
566559 this . log ( "[auth] User in organization context:" , {
567560 id : userMembership . organization . id ,
568561 name : userMembership . organization . name ,
@@ -577,22 +570,15 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
577570 } else {
578571 // Old credentials without organization context - fetch organization info to determine context
579572 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- }
573+ const primaryOrgMembership = this . findPrimaryOrganizationMembership ( orgMemberships )
574+
575+ if ( primaryOrgMembership ) {
576+ this . setUserOrganizationInfo ( userInfo , primaryOrgMembership )
577+ this . log ( "[auth] Legacy credentials: Found organization membership:" , {
578+ id : primaryOrgMembership . organization . id ,
579+ name : primaryOrgMembership . organization . name ,
580+ role : primaryOrgMembership . role ,
581+ } )
596582 } else {
597583 this . log ( "[auth] Legacy credentials: No organization memberships found" )
598584 }
@@ -605,6 +591,26 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
605591 return userInfo
606592 }
607593
594+ private findOrganizationMembership (
595+ memberships : CloudOrganizationMembership [ ] ,
596+ organizationId : string ,
597+ ) : CloudOrganizationMembership | undefined {
598+ return memberships ?. find ( ( membership ) => membership . organization . id === organizationId )
599+ }
600+
601+ private findPrimaryOrganizationMembership (
602+ memberships : CloudOrganizationMembership [ ] ,
603+ ) : CloudOrganizationMembership | undefined {
604+ return memberships && memberships . length > 0 ? memberships [ 0 ] : undefined
605+ }
606+
607+ private setUserOrganizationInfo ( userInfo : CloudUserInfo , membership : CloudOrganizationMembership ) : void {
608+ userInfo . organizationId = membership . organization . id
609+ userInfo . organizationName = membership . organization . name
610+ userInfo . organizationRole = membership . role
611+ userInfo . organizationImageUrl = membership . organization . image_url
612+ }
613+
608614 private async clerkGetOrganizationMemberships ( ) : Promise < CloudOrganizationMembership [ ] > {
609615 const response = await fetch ( `${ getClerkBaseUrl ( ) } /v1/me/organization_memberships` , {
610616 headers : {
0 commit comments