Skip to content

Commit

Permalink
feat(backend-core): Add public_metadata to invitation create API (#85)
Browse files Browse the repository at this point in the history
Invitations can optionally carry metadata that will eventually end up in the created user once they sign up.
The metadata must be a well-formed JSON object. In order to add metadata to an invitation, you can use the
public_metadata property when the invitation is created:

https://docs.clerk.dev/popular-guides/invitations#invitation-metadata
  • Loading branch information
deriegle authored Mar 6, 2022
1 parent cde9725 commit 3c0724a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/backend-core/src/__tests__/apis/InvitationApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,42 @@ test('createInvitation() accepts a redirectUrl', async () => {
);
});

test('createInvitation() accepts publicMetadata', async () => {
const emailAddress = 'test@example.com';
const publicMetadata = {
hello: 'world',
};
const resJSON = {
object: 'invitation',
id: 'inv_randomid',
email_address: emailAddress,
public_metadata: publicMetadata,
created_at: 1611948436,
updated_at: 1611948436,
};

nock('https://api.clerk.dev')
.post('/v1/invitations', {
email_address: emailAddress,
public_metadata: publicMetadata,
})
.reply(200, resJSON);

const invitation = await TestBackendAPIClient.invitations.createInvitation({
emailAddress,
publicMetadata,
});
expect(invitation).toEqual(
new Invitation({
id: resJSON.id,
emailAddress,
publicMetadata,
createdAt: resJSON.created_at,
updatedAt: resJSON.updated_at,
})
);
});

test('revokeInvitation() revokes an invitation', async () => {
const id = 'inv_randomid';
const resJSON = {
Expand Down
1 change: 1 addition & 0 deletions packages/backend-core/src/api/collection/InvitationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const basePath = '/invitations';
type CreateParams = {
emailAddress: string;
redirectUrl?: string;
publicMetadata?: Record<string, unknown>;
};

export class InvitationApi extends AbstractApi {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend-core/src/api/resources/Invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface InvitationPayload extends InvitationProps {}
export interface Invitation extends InvitationPayload {}

export class Invitation {
static attributes = ['id', 'emailAddress', 'createdAt', 'updatedAt'];
static attributes = ['id', 'emailAddress', 'publicMetadata', 'createdAt', 'updatedAt'];

static defaults = [];

Expand Down
1 change: 1 addition & 0 deletions packages/backend-core/src/api/resources/Props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface IdentificationLinkProps extends ClerkProps {

export interface InvitationProps extends ClerkProps {
emailAddress: string;
publicMetadata?: Record<string, unknown>;
createdAt: number;
updatedAt: number;
}
Expand Down

0 comments on commit 3c0724a

Please sign in to comment.