Skip to content

Conversation

@cjbell
Copy link
Contributor

@cjbell cjbell commented Jan 8, 2026

Description

Adds an enabled prop to KnockProvider to control whether the provider should authenticate and initialize the Knock client.

This change addresses the problem where KnockProvider would always attempt authentication on mount, even with undefined userId/userToken, leading to boilerplate conditional rendering or auth errors.

The enabled prop (defaulting to true) allows users to conditionally skip authentication by setting enabled={false}, similar to patterns in other popular React libraries. This removes the need for wrapper components and prevents unnecessary API calls in unauthenticated states.

The implementation refactors KnockProvider into two components: an outer KnockProvider handling the enabled logic and an inner AuthenticatedKnockProvider responsible for the actual client initialization and authentication. This ensures React hook rules are followed while providing the desired functionality.

Checklist

  • Tests have been added for new features or major refactors to existing features.

Linear Issue: KNO-10561

Open in Cursor Open in Web

Co-authored-by: chris <chris@knock.app>
@cursor
Copy link

cursor bot commented Jan 8, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@changeset-bot
Copy link

changeset-bot bot commented Jan 8, 2026

⚠️ No Changeset found

Latest commit: 0f9752c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@linear
Copy link

linear bot commented Jan 8, 2026

@vercel
Copy link

vercel bot commented Jan 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
javascript-ms-teams-connect-example Ready Ready Preview, Comment Jan 8, 2026 9:14pm
javascript-nextjs-example Ready Ready Preview, Comment Jan 8, 2026 9:14pm
javascript-slack-connect-example Ready Ready Preview, Comment Jan 8, 2026 9:14pm
javascript-slack-kit-example Ready Ready Preview, Comment Jan 8, 2026 9:14pm

Co-authored-by: chris <chris@knock.app>
This change ensures that the Knock client is always instantiated, but authentication is conditionally applied based on the `enabled` prop. This simplifies the provider's logic and allows child providers to function even when Knock is disabled.

Co-authored-by: chris <chris@knock.app>
Co-authored-by: chris <chris@knock.app>
Co-authored-by: chris <chris@knock.app>
This commit refactors the logging within the feed client to ensure consistency and improves the authentication logic in the Knock client and KnockProvider.

Co-authored-by: chris <chris@knock.app>
Co-authored-by: chris <chris@knock.app>
Co-authored-by: chris <chris@knock.app>
Co-authored-by: chris <chris@knock.app>
@cjbell
Copy link
Contributor Author

cjbell commented Jan 8, 2026

@cursor review

This commit adds checks to ensure that client methods are only called when the user is authenticated. If the user is not authenticated, the method will log a message and return early, preventing unnecessary API calls.

Co-authored-by: chris <chris@knock.app>
Co-authored-by: chris <chris@knock.app>
@codecov
Copy link

codecov bot commented Jan 8, 2026

Codecov Report

❌ Patch coverage is 80.71749% with 43 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.73%. Comparing base (5c9a2f4) to head (0f9752c).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
packages/client/src/clients/feed/feed.ts 63.33% 22 Missing ⚠️
...src/modules/push/KnockPushNotificationProvider.tsx 0.00% 12 Missing ⚠️
...modules/push/KnockExpoPushNotificationProvider.tsx 25.00% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #820      +/-   ##
==========================================
+ Coverage   67.31%   67.73%   +0.41%     
==========================================
  Files         189      189              
  Lines        7930     8144     +214     
  Branches     1024     1095      +71     
==========================================
+ Hits         5338     5516     +178     
- Misses       2567     2603      +36     
  Partials       25       25              
Files with missing lines Coverage Δ
packages/client/src/clients/guide/client.ts 88.96% <100.00%> (+0.44%) ⬆️
packages/client/src/clients/messages/index.ts 100.00% <100.00%> (ø)
packages/client/src/clients/ms-teams/index.ts 100.00% <100.00%> (ø)
packages/client/src/clients/slack/index.ts 100.00% <100.00%> (ø)
packages/client/src/clients/users/index.ts 100.00% <100.00%> (ø)
packages/client/src/knock.ts 100.00% <100.00%> (ø)
...ct-core/src/modules/core/context/KnockProvider.tsx 97.40% <100.00%> (+1.17%) ⬆️
...modules/push/KnockExpoPushNotificationProvider.tsx 72.40% <25.00%> (-2.92%) ⬇️
...src/modules/push/KnockPushNotificationProvider.tsx 1.06% <0.00%> (-0.16%) ⬇️
packages/client/src/clients/feed/feed.ts 81.14% <63.33%> (-0.65%) ⬇️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants