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

fix(clerk-js,types): Reuse existing sign-up if available #4720

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changeset/purple-foxes-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@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.
- Fix a ticket flow issue on `<SignUp />` component, where in some rare cases the initial ticket/context is lost, because of creating a new sign-up attempt ID.
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
Loading