Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce user method to leave an organization #1809

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/early-tigers-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clerk/clerk-js': minor
'@clerk/types': minor
---

Introduce a new user resource method to leave an organization. You can now call 'user.leaveOrganization(<org_id>)' when a user chooses to leave an organization instead of 'organization.removeMember(<user_id>)' which is mostly meant for organization based actions.
2 changes: 2 additions & 0 deletions .changeset/shaggy-bottles-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
11 changes: 11 additions & 0 deletions packages/clerk-js/src/core/resources/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ export class User extends BaseResource implements UserResource {
getOrganizationMemberships: GetOrganizationMemberships = retrieveMembership =>
OrganizationMembership.retrieve(retrieveMembership);

leaveOrganization = async (organizationId: string): Promise<DeletedObjectResource> => {
const json = (
await BaseResource._fetch<DeletedObjectJSON>({
path: `${this.path()}/organization_memberships/${organizationId}`,
method: 'DELETE',
})
)?.response as unknown as DeletedObjectJSON;

return new DeletedObject(json);
};

get verifiedExternalAccounts() {
return this.externalAccounts.filter(externalAccount => externalAccount.verification?.status == 'verified');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import { OrganizationProfileBreadcrumbs } from './OrganizationProfileNavbar';
export const LeaveOrganizationPage = () => {
const card = useCardState();
const { navigateAfterLeaveOrganization } = useOrganizationProfileContext();
const { organization, membership } = useCoreOrganization();
const { organization } = useCoreOrganization();
const user = useCoreUser();

if (!organization || !membership) {
if (!organization) {
return null;
}

const leave = () => {
return card.runAsync(organization.removeMember(user.id)).then(navigateAfterLeaveOrganization);
return card.runAsync(user.leaveOrganization(organization.id)).then(navigateAfterLeaveOrganization);
};

return (
Expand All @@ -50,9 +50,9 @@ export const LeaveOrganizationPage = () => {
export const DeleteOrganizationPage = () => {
const card = useCardState();
const { navigateAfterLeaveOrganization } = useOrganizationProfileContext();
const { organization, membership } = useCoreOrganization();
const { organization } = useCoreOrganization();

if (!organization || !membership) {
if (!organization) {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions packages/types/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface UserResource extends ClerkResource {
getOrganizationSuggestions: (
params?: GetUserOrganizationSuggestionsParams,
) => Promise<ClerkPaginatedResponse<OrganizationSuggestionResource>>;
leaveOrganization: (organizationId: string) => Promise<DeletedObjectResource>;
createTOTP: () => Promise<TOTPResource>;
verifyTOTP: (params: VerifyTOTPParams) => Promise<TOTPResource>;
disableTOTP: () => Promise<DeletedObjectResource>;
Expand Down