From e363a4e2e6d740a358cd00c8fa88c62628e9a54d Mon Sep 17 00:00:00 2001 From: Konstantinos Pittas Date: Thu, 5 Dec 2024 19:14:25 +0200 Subject: [PATCH] fix(clerk-js,types): Reuse existing sign-up if available --- .changeset/purple-foxes-develop.md | 6 ++++++ packages/clerk-js/src/core/resources/SignUp.ts | 4 ++++ .../clerk-js/src/ui/components/SignUp/SignUpStart.tsx | 11 +++++++++-- packages/types/src/signUp.ts | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changeset/purple-foxes-develop.md diff --git a/.changeset/purple-foxes-develop.md b/.changeset/purple-foxes-develop.md new file mode 100644 index 0000000000..88e86ea8d3 --- /dev/null +++ b/.changeset/purple-foxes-develop.md @@ -0,0 +1,6 @@ +--- +'@clerk/clerk-js': minor +'@clerk/types': minor +--- + +Introduced an `upsert` method to the `SignUp` resource, which reuses the existing sign-up attempt ID if it exists. This was an obvious oversight in the ticket flow, so `SignUpStart` has been updated to use this instead. diff --git a/packages/clerk-js/src/core/resources/SignUp.ts b/packages/clerk-js/src/core/resources/SignUp.ts index 5586e77e86..66b0d81db9 100644 --- a/packages/clerk-js/src/core/resources/SignUp.ts +++ b/packages/clerk-js/src/core/resources/SignUp.ts @@ -367,6 +367,10 @@ export class SignUp extends BaseResource implements SignUpResource { }); }; + upsert = (params: SignUpCreateParams | SignUpUpdateParams): Promise => { + return this.id ? this.update(params) : this.create(params); + }; + validatePassword: ReturnType = (password, cb) => { if (SignUp.clerk.__unstable__environment?.userSettings.passwordSettings) { return createValidatePassword({ diff --git a/packages/clerk-js/src/ui/components/SignUp/SignUpStart.tsx b/packages/clerk-js/src/ui/components/SignUp/SignUpStart.tsx index 2fe85d65af..acdd3e35c1 100644 --- a/packages/clerk-js/src/ui/components/SignUp/SignUpStart.tsx +++ b/packages/clerk-js/src/ui/components/SignUp/SignUpStart.tsx @@ -216,7 +216,14 @@ function _SignUpStart(): JSX.Element { if (fields.ticket) { const noop = () => {}; // fieldsToSubmit: Constructing a fake fields object for strategy. - fieldsToSubmit.push({ id: 'strategy', value: 'ticket', setValue: noop, onChange: noop, setError: noop } as any); + fieldsToSubmit.push({ + id: 'strategy', + value: 'ticket', + clearFeedback: noop, + setValue: noop, + onChange: noop, + setError: noop, + } as any); } // In case of emailOrPhone (both email & phone are optional) and neither of them is provided, @@ -236,7 +243,7 @@ function _SignUpStart(): JSX.Element { const redirectUrlComplete = ctx.afterSignUpUrl || '/'; return signUp - .create(buildRequest(fieldsToSubmit)) + .upsert(buildRequest(fieldsToSubmit)) .then(res => completeSignUpFlow({ signUp: res, diff --git a/packages/types/src/signUp.ts b/packages/types/src/signUp.ts index b98fec79a2..b40a3eb448 100644 --- a/packages/types/src/signUp.ts +++ b/packages/types/src/signUp.ts @@ -66,6 +66,8 @@ export interface SignUpResource extends ClerkResource { update: (params: SignUpUpdateParams) => Promise; + upsert: (params: SignUpCreateParams | SignUpUpdateParams) => Promise; + prepareVerification: (params: PrepareVerificationParams) => Promise; attemptVerification: (params: AttemptVerificationParams) => Promise;