Skip to content
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
31 changes: 31 additions & 0 deletions .changeset/thin-foxes-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'@clerk/backend': patch
---

Adds the following User-centric functionality to the Backend API client.


```ts
import { createClerkClient } from '@clerk/backend';

const clerkClient = createClerkClient(...);

await clerkClient.users.getOrganizationInvitationList({
userId: 'user_xxxxxx',
status: 'pending',
});
await clerkClient.users.deleteUserPasskey({
userId: 'user_xxxxxx',
passkeyIdentificationId: 'xxxxxxx',
});
await clerkClient.users.deleteUserWeb3Wallet({
userId: 'user_xxxxxx',
web3WalletIdentificationId: 'xxxxxxx',
});
await clerkClient.users.deleteUserExternalAccount({
userId: 'user_xxxxxx',
externalAccountId: 'xxxxxxx',
});
await clerkClient.users.deleteUserBackupCodes('user_xxxxxx');
await clerkClient.users.deleteUserTOTP('user_xxxxxx');
```
90 changes: 87 additions & 3 deletions packages/backend/src/api/endpoints/UserApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { ClerkPaginationRequest, OAuthProvider } from '@clerk/types';
import type { ClerkPaginationRequest, OAuthProvider, OrganizationInvitationStatus } from '@clerk/types';

import { runtime } from '../../runtime';
import { joinPaths } from '../../util/path';
import { deprecated } from '../../util/shared';
import type { OauthAccessToken, OrganizationMembership, User } from '../resources';
import type {
DeletedObject,
OauthAccessToken,
OrganizationInvitation,
OrganizationMembership,
User,
} from '../resources';
import type { PaginatedResourceResponse } from '../resources/Deserializer';
import { AbstractAPI } from './AbstractApi';
import type { WithSign } from './util-types';
Expand Down Expand Up @@ -110,6 +116,11 @@ type GetOrganizationMembershipListParams = ClerkPaginationRequest<{
userId: string;
}>;

type GetOrganizationInvitationListParams = ClerkPaginationRequest<{
userId: string;
status?: OrganizationInvitationStatus;
}>;

type VerifyPasswordParams = {
userId: string;
password: string;
Expand All @@ -120,6 +131,25 @@ type VerifyTOTPParams = {
code: string;
};

type DeleteUserPasskeyParams = {
userId: string;
passkeyIdentificationId: string;
};

type DeleteWeb3WalletParams = {
userId: string;
web3WalletIdentificationId: string;
};

type DeleteUserExternalAccountParams = {
userId: string;
externalAccountId: string;
};

type UserID = {
userId: string;
};

export class UserAPI extends AbstractAPI {
public async getUserList(params: UserListParams = {}) {
const { limit, offset, orderBy, ...userCountParams } = params;
Expand Down Expand Up @@ -232,7 +262,7 @@ export class UserAPI extends AbstractAPI {

public async disableUserMFA(userId: string) {
this.requireId(userId);
return this.request<User>({
return this.request<UserID>({
method: 'DELETE',
path: joinPaths(basePath, userId, 'mfa'),
});
Expand All @@ -249,6 +279,17 @@ export class UserAPI extends AbstractAPI {
});
}

public async getOrganizationInvitationList(params: GetOrganizationInvitationListParams) {
const { userId, ...queryParams } = params;
this.requireId(userId);

return this.request<PaginatedResourceResponse<OrganizationInvitation[]>>({
method: 'GET',
path: joinPaths(basePath, userId, 'organization_invitations'),
queryParams,
});
}

public async verifyPassword(params: VerifyPasswordParams) {
const { userId, password } = params;
this.requireId(userId);
Expand Down Expand Up @@ -310,4 +351,47 @@ export class UserAPI extends AbstractAPI {
path: joinPaths(basePath, userId, 'profile_image'),
});
}

public async deleteUserPasskey(params: DeleteUserPasskeyParams) {
this.requireId(params.userId);
this.requireId(params.passkeyIdentificationId);
return this.request<DeletedObject>({
method: 'DELETE',
path: joinPaths(basePath, params.userId, 'passkeys', params.passkeyIdentificationId),
});
}

public async deleteUserWeb3Wallet(params: DeleteWeb3WalletParams) {
this.requireId(params.userId);
this.requireId(params.web3WalletIdentificationId);
return this.request<DeletedObject>({
method: 'DELETE',
path: joinPaths(basePath, params.userId, 'web3_wallets', params.web3WalletIdentificationId),
});
}

public async deleteUserExternalAccount(params: DeleteUserExternalAccountParams) {
this.requireId(params.userId);
this.requireId(params.externalAccountId);
return this.request<DeletedObject>({
method: 'DELETE',
path: joinPaths(basePath, params.userId, 'external_accounts', params.externalAccountId),
});
}

public async deleteUserBackupCodes(userId: string) {
this.requireId(userId);
return this.request<UserID>({
method: 'DELETE',
path: joinPaths(basePath, userId, 'backup_code'),
});
}

public async deleteUserTOTP(userId: string) {
this.requireId(userId);
return this.request<UserID>({
method: 'DELETE',
path: joinPaths(basePath, userId, 'totp'),
});
}
}
3 changes: 3 additions & 0 deletions packages/backend/src/api/resources/JSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,16 @@ export interface OrganizationDomainVerificationJSON {
export interface OrganizationInvitationJSON extends ClerkResourceJSON {
email_address: string;
role: OrganizationMembershipRole;
role_name: string;
organization_id: string;
public_organization_data?: PublicOrganizationDataJSON | null;
status?: OrganizationInvitationStatus;
public_metadata: OrganizationInvitationPublicMetadata;
private_metadata: OrganizationInvitationPrivateMetadata;
url: string | null;
created_at: number;
updated_at: number;
expires_at: number;
}

export interface PublicOrganizationDataJSON extends ClerkResourceJSON {
Expand Down
10 changes: 9 additions & 1 deletion packages/backend/src/api/resources/OrganizationInvitation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OrganizationInvitationStatus, OrganizationMembershipRole } from './Enums';
import type { OrganizationInvitationJSON } from './JSON';
import type { OrganizationInvitationJSON, PublicOrganizationDataJSON } from './JSON';

export class OrganizationInvitation {
private _raw: OrganizationInvitationJSON | null = null;
Expand All @@ -12,25 +12,33 @@ export class OrganizationInvitation {
readonly id: string,
readonly emailAddress: string,
readonly role: OrganizationMembershipRole,
readonly roleName: string,
readonly organizationId: string,
readonly createdAt: number,
readonly updatedAt: number,
readonly expiresAt: number,
readonly url: string | null,
readonly status?: OrganizationInvitationStatus,
readonly publicMetadata: OrganizationInvitationPublicMetadata = {},
readonly privateMetadata: OrganizationInvitationPrivateMetadata = {},
readonly publicOrganizationData?: PublicOrganizationDataJSON | null,
) {}

static fromJSON(data: OrganizationInvitationJSON) {
const res = new OrganizationInvitation(
data.id,
data.email_address,
data.role,
data.role_name,
data.organization_id,
data.created_at,
data.updated_at,
data.expires_at,
data.url,
data.status,
data.public_metadata,
data.private_metadata,
data.public_organization_data,
);
res._raw = data;
return res;
Expand Down