diff --git a/packages/clerk-js/src/core/clerk.ts b/packages/clerk-js/src/core/clerk.ts index 412477a298..bdc3f922be 100644 --- a/packages/clerk-js/src/core/clerk.ts +++ b/packages/clerk-js/src/core/clerk.ts @@ -584,6 +584,18 @@ export default class Clerk implements ClerkInterface { return await Organization.create(name); }; + public getOrganizations = async (): Promise => { + return await Organization.retrieve(); + }; + + public getOrganization = async ( + organizationId: string, + ): Promise => { + return (await Organization.retrieve()).find( + org => org.id === organizationId, + ); + }; + updateClient = (newClient: ClientResource): void => { if (!this.client) { // This is the first time client is being diff --git a/packages/clerk-js/src/core/resources/Organization.ts b/packages/clerk-js/src/core/resources/Organization.ts index b27abd3b17..3f38d1d80e 100644 --- a/packages/clerk-js/src/core/resources/Organization.ts +++ b/packages/clerk-js/src/core/resources/Organization.ts @@ -40,21 +40,20 @@ export class Organization extends BaseResource implements OrganizationResource { return new Organization(json); } - static retrieve( + static async retrieve( getOrganizationParams?: GetOrganizationParams, ): Promise { - return this.clerk - .getFapiClient() - .request({ - method: 'GET', - path: '/me/organizations', - search: getOrganizationParams as any, - }) + return await BaseResource._fetch({ + path: '/me/organizations', + method: 'GET', + search: getOrganizationParams as any, + }) .then(res => { - const organizationsJSON = res.payload - ?.response as unknown as OrganizationJSON[]; + const organizationsJSON = + res?.response as unknown as OrganizationJSON[]; return organizationsJSON.map(org => new Organization(org)); - }); + }) + .catch(() => []); } getMembers = async ( diff --git a/packages/clerk-js/src/ui/signUp/SignUpStart.tsx b/packages/clerk-js/src/ui/signUp/SignUpStart.tsx index b44ef83c5f..d31cb00f5e 100644 --- a/packages/clerk-js/src/ui/signUp/SignUpStart.tsx +++ b/packages/clerk-js/src/ui/signUp/SignUpStart.tsx @@ -95,6 +95,7 @@ function _SignUpStart(): JSX.Element { ? { invitation_token: invitationToken } : { strategy: 'ticket', ticket: organizationInvitationToken }; setIsLoading(true); + signUp .create(invitationParams) .then(res => { @@ -102,7 +103,9 @@ function _SignUpStart(): JSX.Element { void completeSignUpFlow(res); }) .catch(err => { + /* Clear token values when an error occurs in the initial sign up attempt */ formFields.invitationToken.setValue(''); + formFields.organizationInvitationToken.setValue(''); handleError(err, [], setError); }) .finally(() => { diff --git a/packages/react/src/hooks/useOrganizations.ts b/packages/react/src/hooks/useOrganizations.ts index 449537a2e3..d9d6764ab9 100644 --- a/packages/react/src/hooks/useOrganizations.ts +++ b/packages/react/src/hooks/useOrganizations.ts @@ -9,6 +9,10 @@ type UseOrganizations = { createOrganization: ( params: CreateOrganizationParams, ) => Promise; + getOrganizations: () => Promise; + getOrganization: ( + organizationId: string, + ) => Promise; }; export function useOrganizations(): UseOrganizations { @@ -18,5 +22,7 @@ export function useOrganizations(): UseOrganizations { return { createOrganization: clerk.createOrganization, + getOrganizations: clerk.getOrganizations, + getOrganization: clerk.getOrganization, }; } diff --git a/packages/react/src/isomorphicClerk.ts b/packages/react/src/isomorphicClerk.ts index 0334f6a976..126169be89 100644 --- a/packages/react/src/isomorphicClerk.ts +++ b/packages/react/src/isomorphicClerk.ts @@ -467,6 +467,26 @@ export default class IsomorphicClerk { } }; + getOrganizations = async (): Promise => { + const callback = () => this.clerkjs?.getOrganizations(); + if (this.clerkjs && this._loaded) { + return callback() as Promise; + } else { + this.premountMethodCalls.set('getOrganizations', callback); + } + }; + + getOrganization = async ( + organizationId: string, + ): Promise => { + const callback = () => this.clerkjs?.getOrganization(organizationId); + if (this.clerkjs && this._loaded) { + return callback() as Promise; + } else { + this.premountMethodCalls.set('getOrganization', callback); + } + }; + signOut = async (signOutCallback?: SignOutCallback): Promise => { const callback = () => this.clerkjs?.signOut(signOutCallback); if (this.clerkjs && this._loaded) { diff --git a/packages/types/src/clerk.ts b/packages/types/src/clerk.ts index 727517faa9..0584b45754 100644 --- a/packages/types/src/clerk.ts +++ b/packages/types/src/clerk.ts @@ -228,10 +228,25 @@ export interface Clerk { params?: AuthenticateWithMetamaskParams, ) => Promise; + /** + * Creates an organization, adding the current user as admin. + */ createOrganization: ( params: CreateOrganizationParams, ) => Promise; + /** + * Retrieves all the organizations the current user is a member of. + */ + getOrganizations: () => Promise; + + /** + * Retrieves a single organization by id. + */ + getOrganization: ( + organizationId: string, + ) => Promise; + /** * Handles a 401 response from Frontend API by refreshing the client and session object accordingly */