diff --git a/packages/clerk-js/src/core/resources/User.test.ts b/packages/clerk-js/src/core/resources/User.test.ts index 34643b9fc76..1261f9dec77 100644 --- a/packages/clerk-js/src/core/resources/User.test.ts +++ b/packages/clerk-js/src/core/resources/User.test.ts @@ -1,5 +1,5 @@ -import { ExternalAccountJSON, UserJSON, VerificationJSON, Web3WalletJSON } from '@clerk/types'; -import { BaseResource, ExternalAccount } from 'core/resources/internal'; +import { UserJSON } from '@clerk/types'; +import { BaseResource } from 'core/resources/internal'; import { User } from './User'; @@ -64,4 +64,70 @@ describe('User', () => { }, }); }); + + it('denotes if has a verified email address', () => { + const userWithUnverifiedEmail = new User({ + email_addresses: [ + { + emailAddress: 'unverified@clerk.dev', + verification: null, + }, + ], + phone_numbers: [], + web3_wallets: [], + external_accounts: [], + } as unknown as UserJSON); + + expect(userWithUnverifiedEmail.hasVerifiedEmailAddress).toEqual(false); + + const userWithVerifiedEmail = new User({ + email_addresses: [ + { + emailAddress: 'unverified@clerk.dev', + verification: { + status: 'verified', + }, + }, + ], + phone_numbers: [], + web3_wallets: [], + external_accounts: [], + } as unknown as UserJSON); + + expect(userWithVerifiedEmail.hasVerifiedEmailAddress).toEqual(true); + }); + + it('denotes if has a verified phone number', () => { + const userWithUnverifiedPhone = new User({ + email_addresses: [], + phone_numbers: [ + { + phoneNumber: '+306900000000', + verification: { + status: 'unverified', + }, + }, + ], + web3_wallets: [], + external_accounts: [], + } as unknown as UserJSON); + + expect(userWithUnverifiedPhone.hasVerifiedPhoneNumber).toEqual(false); + + const userWithVerifiedPhone = new User({ + email_addresses: [], + phone_numbers: [ + { + phoneNumber: '+306900000000', + verification: { + status: 'verified', + }, + }, + ], + web3_wallets: [], + external_accounts: [], + } as unknown as UserJSON); + + expect(userWithVerifiedPhone.hasVerifiedPhoneNumber).toEqual(true); + }); }); diff --git a/packages/clerk-js/src/core/resources/User.ts b/packages/clerk-js/src/core/resources/User.ts index d0a0fafe628..c8c3e6031a5 100644 --- a/packages/clerk-js/src/core/resources/User.ts +++ b/packages/clerk-js/src/core/resources/User.ts @@ -182,6 +182,14 @@ export class User extends BaseResource implements UserResource { return this.externalAccounts.filter(externalAccount => externalAccount.verification?.status != 'verified'); } + get hasVerifiedEmailAddress() { + return this.emailAddresses.filter(email => email.verification.status === 'verified').length > 0; + } + + get hasVerifiedPhoneNumber() { + return this.phoneNumbers.filter(phone => phone.verification.status === 'verified').length > 0; + } + protected fromJSON(data: UserJSON): this { this.id = data.id; this.externalId = data.external_id; diff --git a/packages/types/src/user.ts b/packages/types/src/user.ts index 32b39347019..8e6a449847d 100644 --- a/packages/types/src/user.ts +++ b/packages/types/src/user.ts @@ -73,6 +73,8 @@ export interface UserResource extends ClerkResource { }) => Promise; get verifiedExternalAccounts(): ExternalAccountResource[]; get unverifiedExternalAccounts(): ExternalAccountResource[]; + get hasVerifiedEmailAddress(): boolean; + get hasVerifiedPhoneNumber(): boolean; } export type CreateEmailAddressParams = { email: string };