@@ -469,6 +469,60 @@ export class WebAuthService extends EventEmitter<AuthServiceEvents> implements A
469469 return this . credentials ?. organizationId || null
470470 }
471471
472+ /**
473+ * Switch to a different organization context
474+ * @param organizationId The organization ID to switch to, or null for personal account
475+ */
476+ public async switchOrganization ( organizationId : string | null ) : Promise < void > {
477+ if ( ! this . credentials ) {
478+ throw new Error ( "Cannot switch organization: not authenticated" )
479+ }
480+
481+ // Update the stored credentials with the new organization ID
482+ const updatedCredentials : AuthCredentials = {
483+ ...this . credentials ,
484+ organizationId : organizationId ,
485+ }
486+
487+ // Store the updated credentials
488+ await this . storeCredentials ( updatedCredentials )
489+
490+ // Update the local credentials
491+ this . credentials = updatedCredentials
492+
493+ // Clear the current session token to force a refresh with new org context
494+ this . sessionToken = null
495+
496+ // Trigger a session refresh to get a new token with the correct org context
497+ try {
498+ await this . refreshSession ( )
499+ // Fetch updated user info after organization switch to reflect new context
500+ await this . fetchUserInfo ( )
501+ } catch ( error ) {
502+ this . log ( `[auth] Failed to refresh session after organization switch: ${ error } ` )
503+ // Even if refresh fails, the credentials are updated for next attempt
504+ }
505+
506+ this . log ( `[auth] Switched organization context to: ${ organizationId || "personal account" } ` )
507+ }
508+
509+ /**
510+ * Get all organization memberships for the current user
511+ * @returns Array of organization memberships
512+ */
513+ public async getOrganizationMemberships ( ) : Promise < CloudOrganizationMembership [ ] > {
514+ if ( ! this . credentials ) {
515+ return [ ]
516+ }
517+
518+ try {
519+ return await this . clerkGetOrganizationMemberships ( )
520+ } catch ( error ) {
521+ this . log ( `[auth] Failed to get organization memberships: ${ error } ` )
522+ return [ ]
523+ }
524+ }
525+
472526 private async clerkSignIn ( ticket : string ) : Promise < AuthCredentials > {
473527 const formData = new URLSearchParams ( )
474528 formData . append ( "strategy" , "ticket" )
0 commit comments