Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types,clerk-js): Introduce user hasVerifiedEmailAddress & hasVerifiedPhoneNumber attributes #300

Merged
merged 1 commit into from
Jul 1, 2022
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
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