Skip to content

Commit

Permalink
fix(clerk-js,types): Reuse existing sign-up if available
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaspt committed Dec 10, 2024
1 parent 52a24eb commit e363a4e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/purple-foxes-develop.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions packages/clerk-js/src/core/resources/SignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ export class SignUp extends BaseResource implements SignUpResource {
});
};

upsert = (params: SignUpCreateParams | SignUpUpdateParams): Promise<SignUpResource> => {
return this.id ? this.update(params) : this.create(params);
};

validatePassword: ReturnType<typeof createValidatePassword> = (password, cb) => {
if (SignUp.clerk.__unstable__environment?.userSettings.passwordSettings) {
return createValidatePassword({
Expand Down
11 changes: 9 additions & 2 deletions packages/clerk-js/src/ui/components/SignUp/SignUpStart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export interface SignUpResource extends ClerkResource {

update: (params: SignUpUpdateParams) => Promise<SignUpResource>;

upsert: (params: SignUpCreateParams | SignUpUpdateParams) => Promise<SignUpResource>;

prepareVerification: (params: PrepareVerificationParams) => Promise<SignUpResource>;

attemptVerification: (params: AttemptVerificationParams) => Promise<SignUpResource>;
Expand Down

0 comments on commit e363a4e

Please sign in to comment.