Skip to content

Commit 2cd53cd

Browse files
authored
fix(clerk-js): Add private _create method for use inside runAsyncResourceTask (#6932)
1 parent c9ea544 commit 2cd53cd

File tree

6 files changed

+72
-36
lines changed

6 files changed

+72
-36
lines changed

.changeset/giant-cats-lay.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/types': minor
4+
---
5+
6+
[Experimental] Fix `signIn.password` emailAddress parameter name.

.changeset/purple-toys-refuse.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/clerk-js': minor
3+
'@clerk/types': minor
4+
---
5+
6+
[Experimental] Fix issue where calling `this.create()` would not correctly propagate errors.

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -703,23 +703,27 @@ class SignInFuture implements SignInFutureResource {
703703
});
704704
}
705705

706+
private async _create(params: SignInFutureCreateParams): Promise<void> {
707+
await this.resource.__internal_basePost({
708+
path: this.resource.pathRoot,
709+
body: params,
710+
});
711+
}
712+
706713
async create(params: SignInFutureCreateParams): Promise<{ error: unknown }> {
707714
return runAsyncResourceTask(this.resource, async () => {
708-
await this.resource.__internal_basePost({
709-
path: this.resource.pathRoot,
710-
body: params,
711-
});
715+
await this._create(params);
712716
});
713717
}
714718

715719
async password(params: SignInFuturePasswordParams): Promise<{ error: unknown }> {
716-
if ([params.identifier, params.email, params.phoneNumber].filter(Boolean).length > 1) {
717-
throw new Error('Only one of identifier, email, or phoneNumber can be provided');
720+
if ([params.identifier, params.emailAddress, params.phoneNumber].filter(Boolean).length > 1) {
721+
throw new Error('Only one of identifier, emailAddress, or phoneNumber can be provided');
718722
}
719723

720724
return runAsyncResourceTask(this.resource, async () => {
721725
// TODO @userland-errors:
722-
const identifier = params.identifier || params.email || params.phoneNumber;
726+
const identifier = params.identifier || params.emailAddress || params.phoneNumber;
723727
const previousIdentifier = this.resource.identifier;
724728
await this.resource.__internal_basePost({
725729
path: this.resource.pathRoot,
@@ -744,7 +748,7 @@ class SignInFuture implements SignInFutureResource {
744748

745749
return runAsyncResourceTask(this.resource, async () => {
746750
if (emailAddress) {
747-
await this.create({ identifier: emailAddress });
751+
await this._create({ identifier: emailAddress });
748752
}
749753

750754
const emailCodeFactor = this.selectFirstFactor({ strategy: 'email_code', emailAddressId });
@@ -785,7 +789,7 @@ class SignInFuture implements SignInFutureResource {
785789

786790
return runAsyncResourceTask(this.resource, async () => {
787791
if (emailAddress) {
788-
await this.create({ identifier: emailAddress });
792+
await this._create({ identifier: emailAddress });
789793
}
790794

791795
const emailLinkFactor = this.selectFirstFactor({ strategy: 'email_link', emailAddressId });
@@ -848,7 +852,7 @@ class SignInFuture implements SignInFutureResource {
848852

849853
return runAsyncResourceTask(this.resource, async () => {
850854
if (phoneNumber) {
851-
await this.create({ identifier: phoneNumber });
855+
await this._create({ identifier: phoneNumber });
852856
}
853857

854858
const phoneCodeFactor = this.selectFirstFactor({ strategy: 'phone_code', phoneNumberId });
@@ -880,14 +884,19 @@ class SignInFuture implements SignInFutureResource {
880884
throw new Error('modal flow is not supported yet');
881885
}
882886

883-
if (!this.resource.id) {
884-
await this.create({
885-
strategy,
886-
redirectUrl: SignIn.clerk.buildUrlWithAuth(redirectCallbackUrl),
887-
actionCompleteRedirectUrl: redirectUrl,
888-
});
887+
let actionCompleteRedirectUrl = redirectUrl;
888+
try {
889+
new URL(redirectUrl);
890+
} catch {
891+
actionCompleteRedirectUrl = window.location.origin + redirectUrl;
889892
}
890893

894+
await this._create({
895+
strategy,
896+
redirectUrl: SignIn.clerk.buildUrlWithAuth(redirectCallbackUrl),
897+
actionCompleteRedirectUrl,
898+
});
899+
891900
const { status, externalVerificationRedirectURL } = this.resource.firstFactorVerification;
892901

893902
if (status === 'unverified' && externalVerificationRedirectURL) {
@@ -924,7 +933,7 @@ class SignInFuture implements SignInFutureResource {
924933
throw new Error(`Unsupported Web3 provider: ${provider}`);
925934
}
926935

927-
await this.create({ identifier });
936+
await this._create({ identifier });
928937

929938
const web3FirstFactor = this.resource.supportedFirstFactors?.find(
930939
f => f.strategy === strategy,

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -660,20 +660,24 @@ class SignUpFuture implements SignUpFutureResource {
660660
return { captchaToken, captchaWidgetType, captchaError };
661661
}
662662

663-
async create(params: SignUpFutureCreateParams): Promise<{ error: unknown }> {
664-
return runAsyncResourceTask(this.resource, async () => {
665-
const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();
663+
private async _create(params: SignUpFutureCreateParams): Promise<void> {
664+
const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();
665+
666+
const body: Record<string, unknown> = {
667+
transfer: params.transfer,
668+
captchaToken,
669+
captchaWidgetType,
670+
captchaError,
671+
...params,
672+
unsafeMetadata: params.unsafeMetadata ? normalizeUnsafeMetadata(params.unsafeMetadata) : undefined,
673+
};
666674

667-
const body: Record<string, unknown> = {
668-
transfer: params.transfer,
669-
captchaToken,
670-
captchaWidgetType,
671-
captchaError,
672-
...params,
673-
unsafeMetadata: params.unsafeMetadata ? normalizeUnsafeMetadata(params.unsafeMetadata) : undefined,
674-
};
675+
await this.resource.__internal_basePost({ path: this.resource.pathRoot, body });
676+
}
675677

676-
await this.resource.__internal_basePost({ path: this.resource.pathRoot, body });
678+
async create(params: SignUpFutureCreateParams): Promise<{ error: unknown }> {
679+
return runAsyncResourceTask(this.resource, async () => {
680+
await this._create(params);
677681
});
678682
}
679683

@@ -756,12 +760,20 @@ class SignUpFuture implements SignUpFutureResource {
756760
const { strategy, redirectUrl, redirectCallbackUrl } = params;
757761
return runAsyncResourceTask(this.resource, async () => {
758762
const { captchaToken, captchaWidgetType, captchaError } = await this.getCaptchaToken();
763+
764+
let redirectUrlComplete = redirectUrl;
765+
try {
766+
new URL(redirectUrl);
767+
} catch {
768+
redirectUrlComplete = window.location.origin + redirectUrl;
769+
}
770+
759771
await this.resource.__internal_basePost({
760772
path: this.resource.pathRoot,
761773
body: {
762774
strategy,
763775
redirectUrl: SignUp.clerk.buildUrlWithAuth(redirectCallbackUrl),
764-
redirectUrlComplete: redirectUrl,
776+
redirectUrlComplete,
765777
captchaToken,
766778
captchaWidgetType,
767779
captchaError,
@@ -808,7 +820,7 @@ class SignUpFuture implements SignUpFutureResource {
808820

809821
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
810822
const web3Wallet = identifier || this.resource.web3wallet!;
811-
await this.create({ web3Wallet, unsafeMetadata, legalAccepted });
823+
await this._create({ web3Wallet, unsafeMetadata, legalAccepted });
812824
await this.resource.__internal_basePost({
813825
body: { strategy },
814826
action: 'prepare_verification',

packages/types/src/signInFuture.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ export interface SignInFutureCreateParams {
1515

1616
export type SignInFuturePasswordParams =
1717
| {
18-
identifier: string;
1918
password: string;
20-
email?: never;
19+
identifier: string;
20+
emailAddress?: never;
2121
phoneNumber?: never;
2222
}
2323
| {
2424
password: string;
25-
email: string;
25+
emailAddress: string;
2626
identifier?: never;
2727
phoneNumber?: never;
2828
}
2929
| {
3030
password: string;
3131
phoneNumber: string;
3232
identifier?: never;
33-
email?: never;
33+
emailAddress?: never;
3434
}
3535
| {
3636
password: string;
3737
phoneNumber?: never;
3838
identifier?: never;
39-
email?: never;
39+
emailAddress?: never;
4040
};
4141

4242
export type SignInFutureEmailCodeSendParams =

packages/types/src/signUpFuture.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ interface SignUpFutureAdditionalParams {
1111
}
1212

1313
export interface SignUpFutureCreateParams extends SignUpFutureAdditionalParams {
14+
emailAddress?: string;
15+
phoneNumber?: string;
16+
username?: string;
1417
transfer?: boolean;
1518
ticket?: string;
1619
web3Wallet?: string;

0 commit comments

Comments
 (0)