Skip to content

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

Merged
tmilewski merged 7 commits into
mainfrom
sdk-1043-assert-clerk-provider
Dec 12, 2023
Merged

chore(clerk-js,clerk-react,shared): Add hook to assert the presence of ClerkProvider [SDK-1043]#2299
tmilewski merged 7 commits into
mainfrom
sdk-1043-assert-clerk-provider

Conversation

@tmilewski

Copy link
Copy Markdown
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

@tmilewskitmilewski self-assigned this Dec 8, 2023
@changeset-bot

changeset-botBot commented Dec 8, 2023

Copy link
Copy Markdown

🦋 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
NameType
@clerk/clerk-jsPatch
@clerk/sharedPatch
@clerk/clerk-reactPatch
@clerk/chrome-extensionPatch
@clerk/clerk-expoPatch
@clerk/backendPatch
@clerk/fastifyPatch
@clerk/nextjsPatch
@clerk/remixPatch
@clerk/clerk-sdk-nodePatch
gatsby-plugin-clerkPatch

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

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ 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

@tmilewskitmilewski 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
Comment thread.changeset/slow-bugs-exist.md Outdated
Comment threadpackages/shared/src/error.ts Outdated
Comment threadpackages/shared/src/react/contexts.tsx Outdated

@dimkldimkl left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
MemberAuthor

❓ 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.

@dimkldimkl left a comment

Copy link
Copy Markdown
Contributor

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:

Comment threadpackages/shared/src/react/contexts.tsx Outdated
Comment threadpackages/shared/src/react/contexts.tsx
@tmilewski
tmilewskiforce-pushed the sdk-1043-assert-clerk-provider branch from d4267a5 to 49e6d01CompareDecember 12, 2023 00:09
Comment threadpackages/shared/src/react/contexts.tsx Outdated
Comment threadpackages/shared/src/error.ts Outdated
Comment thread.changeset/slow-bugs-exist.md Outdated

@LekoArtsLekoArts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@tmilewski
tmilewski added this pull request to the merge queue Dec 12, 2023
Merged via the queue into main with commit 75ea300Dec 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 <lekoarts@gmail.com>
Sign up for freeto 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

@tmilewski@brkalow@dimkl@LekoArts@clerk-cookie