@@ -141,7 +141,8 @@ export class WebAuthService extends EventEmitter<AuthServiceEvents> implements A
141141 if (
142142 this . credentials === null ||
143143 this . credentials . clientToken !== credentials . clientToken ||
144- this . credentials . sessionId !== credentials . sessionId
144+ this . credentials . sessionId !== credentials . sessionId ||
145+ this . credentials . organizationId !== credentials . organizationId
145146 ) {
146147 this . transitionToAttemptingSession ( credentials )
147148 }
@@ -174,6 +175,7 @@ export class WebAuthService extends EventEmitter<AuthServiceEvents> implements A
174175
175176 this . changeState ( "attempting-session" )
176177
178+ this . timer . stop ( )
177179 this . timer . start ( )
178180 }
179181
@@ -469,6 +471,42 @@ export class WebAuthService extends EventEmitter<AuthServiceEvents> implements A
469471 return this . credentials ?. organizationId || null
470472 }
471473
474+ /**
475+ * Switch to a different organization context
476+ * @param organizationId The organization ID to switch to, or null for personal account
477+ */
478+ public async switchOrganization ( organizationId : string | null ) : Promise < void > {
479+ if ( ! this . credentials ) {
480+ throw new Error ( "Cannot switch organization: not authenticated" )
481+ }
482+
483+ // Update the stored credentials with the new organization ID
484+ const updatedCredentials : AuthCredentials = {
485+ ...this . credentials ,
486+ organizationId : organizationId ,
487+ }
488+
489+ // Store the updated credentials, handleCredentialsChange will handle the update
490+ await this . storeCredentials ( updatedCredentials )
491+ }
492+
493+ /**
494+ * Get all organization memberships for the current user
495+ * @returns Array of organization memberships
496+ */
497+ public async getOrganizationMemberships ( ) : Promise < CloudOrganizationMembership [ ] > {
498+ if ( ! this . credentials ) {
499+ return [ ]
500+ }
501+
502+ try {
503+ return await this . clerkGetOrganizationMemberships ( )
504+ } catch ( error ) {
505+ this . log ( `[auth] Failed to get organization memberships: ${ error } ` )
506+ return [ ]
507+ }
508+ }
509+
472510 private async clerkSignIn ( ticket : string ) : Promise < AuthCredentials > {
473511 const formData = new URLSearchParams ( )
474512 formData . append ( "strategy" , "ticket" )
@@ -653,9 +691,14 @@ export class WebAuthService extends EventEmitter<AuthServiceEvents> implements A
653691 }
654692
655693 private async clerkGetOrganizationMemberships ( ) : Promise < CloudOrganizationMembership [ ] > {
694+ if ( ! this . credentials ) {
695+ this . log ( "[auth] Cannot get organization memberships: missing credentials" )
696+ return [ ]
697+ }
698+
656699 const response = await fetch ( `${ getClerkBaseUrl ( ) } /v1/me/organization_memberships` , {
657700 headers : {
658- Authorization : `Bearer ${ this . credentials ! . clientToken } ` ,
701+ Authorization : `Bearer ${ this . credentials . clientToken } ` ,
659702 "User-Agent" : this . userAgent ( ) ,
660703 } ,
661704 signal : AbortSignal . timeout ( 10000 ) ,
0 commit comments