Skip to content

chore(clerk-js,clerk-react,shared): Add hook to assert the presence of ClerkProvider [SDK-1043] #2299

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

Merged
merged 7 commits into from
Dec 12, 2023

Conversation

tmilewski
Copy link
Member

Description

Adds and implements useAssertWrappedByClerkProvider in order to assert that ClerkProvider is present. The goal of this being that we'd like to provide end-users a better development experience.

This applies to all public-facing components and hooks.

The intention behind introducing a new context was to limit the amount of refactoring needed to support what we were looking to achieve.

Current error messaging:

AuthContext not found

New error messaging:

@clerk/clerk-react: useAuth must be used within the <ClerkProvider> component. Please see: https://clerk. com/docs/components/clerk-provider

SDK-1043

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:

Packages affected

  • @clerk/backend
  • @clerk/chrome-extension
  • @clerk/clerk-js
  • @clerk/clerk-expo
  • @clerk/fastify
  • gatsby-plugin-clerk
  • @clerk/localizations
  • @clerk/nextjs
  • @clerk/clerk-react
  • @clerk/remix
  • @clerk/clerk-sdk-node
  • @clerk/shared
  • @clerk/themes
  • @clerk/types
  • build/tooling/chore

@tmilewski tmilewski requested a review from brkalow December 8, 2023 20:36
@tmilewski tmilewski self-assigned this Dec 8, 2023
Copy link

changeset-bot bot commented Dec 8, 2023

🦋 Changeset detected

Latest commit: 06115b2

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

This PR includes changesets to release 11 packages
Name Type
@clerk/clerk-js Patch
@clerk/shared Patch
@clerk/clerk-react Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch
@clerk/backend Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/remix Patch
@clerk/clerk-sdk-node Patch
gatsby-plugin-clerk 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

Copy link
Contributor

github-actions bot commented Dec 8, 2023

⚠️ Changes detected under the ClerkJS ui directory!

Don't forget to apply the same changes under the /ui.retheme directory:
packages/clerk-js/src/ui/** ➡️ packages/clerk-js/src/ui.retheme/**

Also, you may need to update the following files:

  • packages/localizations/src/en-US.retheme.ts
  • packages/localizations/src/index.retheme.ts
  • packages/types/src/appearance.retheme.ts
  • packages/types/src/clerk.retheme.ts
  • packages/types/src/index.retheme.ts
  • packages/types/src/localization.retheme.ts

@tmilewski tmilewski changed the title chore(clerk-js,shared): Add hook to assert the presence of ClerkProvider [SDK-1043] chore(clerk-js,clerk-react,shared): Add hook to assert the presence of ClerkProvider [SDK-1043] Dec 8, 2023
Copy link
Contributor

@dimkl dimkl left a comment

Choose a reason for hiding this comment

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

❓ Instead of adding another Context eg ClerkProviderAssertionContext why not use useIsomorphicClerkContext or useClientContext and apply the same pattern as other hooks?

  const ctx = useClientContext();
  assertContextExists(ctx, ClientContext);

The assertContextExists already exists in the shared package.

@tmilewski
Copy link
Member Author

❓ Instead of adding another Context eg ClerkProviderAssertionContext why not use useIsomorphicClerkContext or useClientContext and apply the same pattern as other hooks?

  const ctx = useClientContext();
  assertContextExists(ctx, ClientContext);

The assertContextExists already exists in the shared package.

@dimkl So, the interesting thing about that example is that, if ClientContext doesn't exist, it'll never get to assertContextExists as it throws, by default, in useClientContext.

That should probably be fixed up, TBD. 👍

Happy to use ClerkInstanceContext/useIsomorphicClerkContext, but we'll have to use the useCtxWithoutGuarantee version of the createContextAndHook so as to not throw by default within the useCtx the created hook, allowing us to throw an appropriate error.

I'll fix up the existing usage mentioned above with this updated approach.

Copy link
Contributor

@dimkl dimkl left a comment

Choose a reason for hiding this comment

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

As part of another PR, I think we also need to consolidate those:

@tmilewski tmilewski force-pushed the sdk-1043-assert-clerk-provider branch from d4267a5 to 49e6d01 Compare December 12, 2023 00:09
Copy link
Member

@LekoArts LekoArts left a comment

Choose a reason for hiding this comment

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

LGTM

@tmilewski tmilewski enabled auto-merge December 12, 2023 14:49
@tmilewski tmilewski added this pull request to the merge queue Dec 12, 2023
Merged via the queue into main with commit 75ea300 Dec 12, 2023
@tmilewski tmilewski deleted the sdk-1043-assert-clerk-provider branch December 12, 2023 15:04
octoper pushed a commit that referenced this pull request Dec 13, 2023
…f ClerkProvider [SDK-1043] (#2299)

* chore(clerk-js,shared): Add hook to assert the presence of ClerkProvider

* chore(clerk-js,clerk-react,shared): Add changeset

* chore(shared): Update messaging

* chore(clerk-js,shared): Re-used existing context

* chore(shared): Require useAssertWrappedByClerkProvider param

* Update .changeset/slow-bugs-exist.md

* chore(shared): Update error messaging

---------

Co-authored-by: Lennart <[email protected]>
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.

5 participants