Skip to content

chore(shared): Update signature of useReverification for error handling configuration - #4564

Merged
panteliselef merged 20 commits into
mainfrom
elef/user-1039-usereverification-should-not-throw-on-cancel-by-default
Nov 20, 2024
Merged

chore(shared): Update signature of useReverification for error handling configuration#4564
panteliselef merged 20 commits into
mainfrom
elef/user-1039-usereverification-should-not-throw-on-cancel-by-default

Conversation

@panteliselef

@panteliselef panteliselef commented Nov 14, 2024

Copy link
Copy Markdown
Contributor

Description

Given this server action

export const deletePatientAction = async (
  prevState: { success: boolean },
  name: string
) => {
  const { orgId, has } = await auth();

  if (!has({ reverification: "strict" })) {
    return reverificationError("strict");
  }

  await waitFor();
  removePatient(name, orgId!);

  return { success: true };
};

// ^^ its return type :
Promise<{
    clerk_error: {
        type: "forbidden";
        reason: "reverification-mismatch";
        metadata: {
            reverification: "strict" | undefined;
        };
    };
} | {
    success: boolean;
}

when it is used as

const [deletePatient] = useReverification(deletePatientAction);
      // ^^ (prevState: {success: boolean;}, name: string) => Promise<{ success: boolean} | null>

but if you use it like with throwOnCancel: true then null will not never be returned since we throw.

const [deletePatient] = useReverification(deletePatientAction, {throwOnCancel: true});
        // ^^ (prevState: {success: boolean;}, name: string) => Promise<{ success: boolean}>

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@panteliselef panteliselef self-assigned this Nov 14, 2024
@changeset-bot

changeset-bot Bot commented Nov 14, 2024

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 43ee306

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 19 packages
Name Type
@clerk/shared Minor
@clerk/astro Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/clerk-expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/clerk-react Patch
@clerk/remix Patch
@clerk/clerk-sdk-node Patch
@clerk/tanstack-start Patch
@clerk/testing Patch
@clerk/ui Patch
@clerk/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@panteliselef
panteliselef marked this pull request as draft November 14, 2024 18:35
@panteliselef
panteliselef marked this pull request as ready for review November 15, 2024 16:30
@panteliselef
panteliselef requested review from a team, brkalow and nikosdouvlis November 15, 2024 16:30
@panteliselef panteliselef changed the title [WIP] chore(shared): Update signature of useReverification for error handling configuration chore(shared): Update signature of useReverification for error handling configuration Nov 15, 2024
Comment thread packages/shared/src/react/hooks/useReverification.ts
Comment thread packages/shared/src/react/hooks/useReverification.ts Outdated
Comment thread packages/shared/src/react/hooks/useReverification.ts Outdated
Comment thread packages/shared/src/react/hooks/useReverification.ts
Comment thread packages/react/src/hooks/__tests__/useReverification.test.ts
@panteliselef
panteliselef requested a review from brkalow November 15, 2024 18:16
@panteliselef
panteliselef requested a review from a team November 15, 2024 18:16
…should-not-throw-on-cancel-by-default

# Conflicts:
#	packages/shared/src/react/hooks/useReverification.ts
This allows for new references to be used, when something else is causing the re-render

@panteliselef panteliselef left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers, do you think this is acceptable ?

const [createEmailAddress] = useReverification(() => user?.createEmailAddress({ email: emailField.value }));

...

const onSubmit = async () => {
 await createEmailAddress()
}

or we should force this ?

const [updatePasswordWithReverification] = useReverification(
    (user: UserResource, opts: Parameters<UserResource['updatePassword']>) => user.updatePassword(...opts),
  );
  
...
  
   
const onSubmit = async () => {
  try {
    const opts = {
      newPassword: passwordField.value,
      signOutOfOtherSessions: sessionsField.checked,
      currentPassword: user.passwordEnabled ? currentPasswordField.value : undefined,
    } satisfies Parameters<typeof user.updatePassword>[0];

    await updatePasswordWithReverification(user, [opts]);
    onSuccess();
  } catch (e) {
    handleError(e);
  }
};

})(fetcherRef.current);
return [handler] as const;
}, [__experimental_openUserVerification, fetcherRef.current]);
}, [__experimental_openUserVerification, fetcherRef.current, optionsRef.current]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the references in here, will not cause the useMemo to re-render explicitly, but if something else causes the Component that the hook is mounted on, this will use the new references if they changed

* }
*/
function __experimental_useReverification<
Fetcher extends (...args: any[]) => Promise<any> | undefined,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added undefined here, in order to support the following

const {user} = useUser()
const [update] = useReverification(()=>user?.update({userName})


async function resolveResult<T>(
result: Promise<T>,
result: Promise<T> | T,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accept the value itself, e.g. undefined instead of Promise<undefined>

@panteliselef
panteliselef merged commit 6b09617 into main Nov 20, 2024
@panteliselef
panteliselef deleted the elef/user-1039-usereverification-should-not-throw-on-cancel-by-default branch November 20, 2024 16:50
wobsoriano pushed a commit that referenced this pull request Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants