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
27 changes: 27 additions & 0 deletions packages/clerk-js/src/core/resources/BackupCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BackupCodeJSON, BackupCodeResource } from '@clerk/types';

import { unixEpochToDate } from '../../utils/date';
import { BaseResource } from './internal';

export class BackupCode extends BaseResource implements BackupCodeResource {
pathRoot = '/me';

id!: string;
codes: string[] = [];
updatedAt: Date | null = null;
createdAt: Date | null = null;

constructor(data: BackupCodeJSON) {
super();
this.fromJSON(data);
}

protected fromJSON(data: BackupCodeJSON): this {
this.id = data.id;
this.codes = data.codes;
this.updatedAt = unixEpochToDate(data.updated_at);
this.createdAt = unixEpochToDate(data.created_at);

return this;
}
}
26 changes: 26 additions & 0 deletions packages/clerk-js/src/core/resources/User.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,30 @@ describe('User', () => {
path: '/me/totp',
});
});

it('creates backup codes', async () => {
const backupCodeJSON = {
object: 'backup_code',
id: 'bcode_1234',
codes: ['1234', '5678'],
};

// @ts-ignore
BaseResource._fetch = jest.fn().mockReturnValue(Promise.resolve({ response: backupCodeJSON }));

const user = new User({
email_addresses: [],
phone_numbers: [],
web3_wallets: [],
external_accounts: [],
} as unknown as UserJSON);

await user.createBackupCode();

// @ts-ignore
expect(BaseResource._fetch).toHaveBeenCalledWith({
method: 'POST',
path: '/me/backup_codes/',
});
});
});
14 changes: 14 additions & 0 deletions packages/clerk-js/src/core/resources/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {
BackupCodeJSON,
BackupCodeResource,
CreateEmailAddressParams,
CreatePhoneNumberParams,
CreateWeb3WalletParams,
Expand All @@ -23,6 +25,7 @@ import type {

import { unixEpochToDate } from '../../utils/date';
import { normalizeUnsafeMetadata } from '../../utils/resourceParams';
import { BackupCode } from './BackupCode';
import {
BaseResource,
DeletedObject,
Expand Down Expand Up @@ -177,6 +180,17 @@ export class User extends BaseResource implements UserResource {
return new DeletedObject(json);
};

createBackupCode = async (): Promise<BackupCodeResource> => {
const json = (
await BaseResource._fetch<BackupCodeJSON>({
path: this.path() + '/backup_codes/',
method: 'POST',
})
)?.response as unknown as BackupCodeJSON;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an issue with this specific PR, but the type casting/ checking for the verifyTOTP, disableTOTP and createBackupCode is definitely brittle and since it involves network requests, the actual runtime value might be different that what we expect at this point

I did a very quick pass and I think that instead of the manual type casts, I'd rather use an assertion function to at least throw/ try to recover at that point. What do you think?

This can definitely be done in a separate PR as its not related with the changes made by Haris :)

cc @SokratisVidros

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


return new BackupCode(json);
};

update = (params: UpdateUserParams): Promise<UserResource> => {
return this._basePatch({
body: normalizeUnsafeMetadata(params),
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/backupCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ClerkResource } from './resource';

export interface BackupCodeResource extends ClerkResource {
id: string;
codes: string[];
createdAt: Date | null;
updatedAt: Date | null;
}
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './api';
export * from './authConfig';
export * from './backupCode';
export * from './clerk';
export * from './client';
export * from './deletedObject';
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ export interface TOTPJSON extends ClerkResourceJSON {
updated_at: number;
}

export interface BackupCodeJSON extends ClerkResourceJSON {
object: 'backup_code';
id: string;
codes: string[];
created_at: number;
updated_at: number;
}

export interface DeletedObjectJSON {
object: string;
id?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BackupCodeResource } from './backupCode';
import { DeletedObjectResource } from './deletedObject';
import { EmailAddressResource } from './emailAddress';
import { ExternalAccountResource } from './externalAccount';
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface UserResource extends ClerkResource {
createTOTP: () => Promise<TOTPResource>;
verifyTOTP: (params: VerifyTOTPParams) => Promise<TOTPResource>;
disableTOTP: () => Promise<DeletedObjectResource>;
createBackupCode: () => Promise<BackupCodeResource>;
get verifiedExternalAccounts(): ExternalAccountResource[];
get unverifiedExternalAccounts(): ExternalAccountResource[];
get hasVerifiedEmailAddress(): boolean;
Expand Down