()
| 594 | } |
| 595 | |
| 596 | private async clerkMe(): Promise<CloudUserInfo> { |
| 597 | const response = await fetch(`${getClerkBaseUrl()}/v1/me`, { |
| 598 | headers: { |
| 599 | Authorization: `Bearer ${this.credentials!.clientToken}`, |
| 600 | "User-Agent": this.userAgent(), |
| 601 | }, |
| 602 | signal: AbortSignal.timeout(10000), |
| 603 | }) |
| 604 | |
| 605 | if (!response.ok) { |
| 606 | throw new Error(`HTTP ${response.status}: ${response.statusText}`) |
| 607 | } |
| 608 | |
| 609 | const payload = await response.json() |
| 610 | const { response: userData } = clerkMeResponseSchema.parse(payload) |
| 611 | |
| 612 | const userInfo: CloudUserInfo = { |
| 613 | id: userData.id, |
| 614 | picture: userData.image_url, |
| 615 | } |
| 616 | |
| 617 | const names = [userData.first_name, userData.last_name].filter((name) => !!name) |
| 618 | userInfo.name = names.length > 0 ? names.join(" ") : undefined |
| 619 | const primaryEmailAddressId = userData.primary_email_address_id |
| 620 | const emailAddresses = userData.email_addresses |
| 621 | |
| 622 | if (primaryEmailAddressId && emailAddresses) { |
| 623 | userInfo.email = emailAddresses.find( |
| 624 | (email: { id: string }) => primaryEmailAddressId === email.id, |
| 625 | )?.email_address |
| 626 | } |
| 627 | |
| 628 | // Fetch organization info if user is in organization context |
| 629 | try { |
| 630 | const storedOrgId = this.getStoredOrganizationId() |
| 631 | |
| 632 | if (this.credentials?.organizationId !== undefined) { |
| 633 | // We have organization context info |
| 634 | if (storedOrgId !== null) { |
| 635 | // User is in organization context - fetch user's memberships and filter |
| 636 | const orgMemberships = await this.clerkGetOrganizationMemberships() |
| 637 | const userMembership = this.findOrganizationMembership(orgMemberships, storedOrgId) |
| 638 | |
| 639 | if (userMembership) { |
| 640 | this.setUserOrganizationInfo(userInfo, userMembership) |
| 641 | |
| 642 | this.log("[auth] User in organization context:", { |
| 643 | id: userMembership.organization.id, |
| 644 | name: userMembership.organization.name, |
| 645 | role: userMembership.role, |
| 646 | }) |
| 647 | } else { |
| 648 | this.log("[auth] Warning: User not found in stored organization:", storedOrgId) |
| 649 | } |
| 650 | } else { |
| 651 | this.log("[auth] User in personal account context - not setting organization info") |
| 652 | } |
| 653 | } else { |
no test coverage detected