Skip to content

Commit

Permalink
Merge pull request #300 from clerkinc/user-attribute-verified-email-p…
Browse files Browse the repository at this point in the history
…hone

feat(types,clerk-js): Introduce user hasVerifiedEmailAddress & hasVerifiedPhoneNumber attributes
  • Loading branch information
chanioxaris authored Jul 1, 2022
2 parents 214b959 + ea68447 commit fb0c00d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
70 changes: 68 additions & 2 deletions packages/clerk-js/src/core/resources/User.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
});
8 changes: 8 additions & 0 deletions packages/clerk-js/src/core/resources/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export interface UserResource extends ClerkResource {
}) => Promise<ExternalAccountResource>;
get verifiedExternalAccounts(): ExternalAccountResource[];
get unverifiedExternalAccounts(): ExternalAccountResource[];
get hasVerifiedEmailAddress(): boolean;
get hasVerifiedPhoneNumber(): boolean;
}

export type CreateEmailAddressParams = { email: string };
Expand Down

0 comments on commit fb0c00d

Please sign in to comment.