Skip to content

Commit 61944c2

Browse files
authored
refactor(clerk-js,types): Add phoneNumber & email to password method (#6661)
1 parent 7c3c1f7 commit 61944c2

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

.changeset/rare-results-leave.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/clerk-js/src/core/resources/SignIn.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,16 @@ class SignInFuture implements SignInFutureResource {
635635
}
636636

637637
async password(params: SignInFuturePasswordParams): Promise<{ error: unknown }> {
638-
const { identifier, password } = params;
638+
if ([params.identifier, params.email, params.phoneNumber].filter(Boolean).length > 1) {
639+
throw new Error('Only one of identifier, email, or phoneNumber can be provided');
640+
}
641+
639642
return runAsyncResourceTask(this.resource, async () => {
643+
const identifier = params.identifier || params.email || params.phoneNumber;
640644
const previousIdentifier = this.resource.identifier;
641645
await this.resource.__internal_basePost({
642646
path: this.resource.pathRoot,
643-
body: { identifier: identifier || previousIdentifier, password },
647+
body: { identifier: identifier || previousIdentifier, password: params.password },
644648
});
645649
});
646650
}

packages/clerk-js/src/core/resources/SignUp.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,21 +595,30 @@ class SignUpFuture implements SignUpFutureResource {
595595
}
596596

597597
async password(params: SignUpFuturePasswordParams): Promise<{ error: unknown }> {
598-
const { emailAddress, password } = params;
598+
if ([params.emailAddress, params.phoneNumber].filter(Boolean).length > 1) {
599+
throw new Error('Only one of emailAddress or phoneNumber can be provided');
600+
}
601+
599602
return runAsyncResourceTask(this.resource, async () => {
600603
const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();
601604

602-
await this.resource.__internal_basePost({
603-
path: this.resource.pathRoot,
604-
body: {
605-
strategy: 'password',
606-
emailAddress,
607-
password,
608-
captchaToken,
609-
captchaWidgetType,
610-
captchaError,
611-
},
612-
});
605+
const body: Record<string, unknown> = {
606+
strategy: 'password',
607+
password: params.password,
608+
captchaToken,
609+
captchaWidgetType,
610+
captchaError,
611+
};
612+
613+
if (params.phoneNumber) {
614+
body.phoneNumber = params.phoneNumber;
615+
}
616+
617+
if (params.emailAddress) {
618+
body.emailAddress = params.emailAddress;
619+
}
620+
621+
await this.resource.__internal_basePost({ path: this.resource.pathRoot, body });
613622
});
614623
}
615624

packages/types/src/signInFuture.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,25 @@ export interface SignInFutureCreateParams {
1111
transfer?: boolean;
1212
}
1313

14-
export interface SignInFuturePasswordParams {
15-
identifier?: string;
16-
password: string;
17-
}
14+
export type SignInFuturePasswordParams =
15+
| {
16+
identifier: string;
17+
password: string;
18+
email?: never;
19+
phoneNumber?: never;
20+
}
21+
| {
22+
password: string;
23+
email: string;
24+
identifier?: never;
25+
phoneNumber?: never;
26+
}
27+
| {
28+
password: string;
29+
phoneNumber: string;
30+
identifier?: never;
31+
email?: never;
32+
};
1833

1934
export interface SignInFutureEmailCodeSendParams {
2035
email: string;

packages/types/src/signUpFuture.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ export interface SignUpFutureEmailCodeVerifyParams {
1010
code: string;
1111
}
1212

13-
export interface SignUpFuturePasswordParams {
14-
emailAddress: string;
15-
password: string;
16-
}
13+
export type SignUpFuturePasswordParams =
14+
| {
15+
emailAddress: string;
16+
password: string;
17+
phoneNumber?: never;
18+
}
19+
| {
20+
phoneNumber: string;
21+
password: string;
22+
emailAddress?: never;
23+
};
1724

1825
export interface SignUpFuturePhoneCodeSendParams {
1926
phoneNumber?: string;

0 commit comments

Comments
 (0)