Add an enabled prop to KnockProvider#1033
Conversation
Add a subscribable authStore + authStatus getter and a Knock.logout() that tears down all stateful connections (socket, token-expiration timer, and page-visibility listener) and lazily re-creates the API client. Make user-scoped calls quiescent while unauthenticated instead of throwing or firing blind: - Feed markAs*/markAll*/fetchNextPage no-op without an optimistic store write. - Guide fetch/subscribe/step-marks no longer throw (fixes the crash when Guides render before a user is set). - Slack/MS Teams authCheck return a disconnected shape; getChannels/getTeams return empty; messages.batchUpdateStatuses returns []. Also fix two guide bugs the enabled prop exacerbates: re-read the socket from the API client on each subscribe so real-time survives re-auth, and share the history pushState/replaceState patch per window so remounting a guide provider no longer nests patches or leaves the originals unrestored.
Implement `enabled` (default true) as credential-nulling through useAuthenticatedKnockClient: when false the client is created but left unauthenticated and fully quiescent while children still render. A fresh client is built on every enable/disable transition so enabling remounts the feed subtree (and refetches) and disabling clears the previous user's stores. Also guard useFeedSettings against firing GET /v1/users/undefined/.../settings for an unauthenticated user, and tear the client down on provider unmount (StrictMode-safe via a dispose/re-init flag) instead of leaking the socket, token timer, and page-visibility listener.
Add useKnockAuthState(knock), which subscribes to the client's authStore and re-renders on login, logout, or a user switch. Wire it into the integrations that previously latched their state: - Slack/MS Teams connection status resets and re-runs authCheck when the authenticated user changes, and the provider keys now include the userId so a user switch reliably re-renders consumers. - Expo autoRegister waits for an authenticated user before registering a push token, deferring the OS permission prompt for logged-out users and re-registering on sign-in; a notification tapped while logged out no longer fires a message-status update.
Add the readyToTarget x enabled/auth matrix to KnockGuideProvider's JSDoc, and document the enabled prop in the react, react-native, and expo READMEs plus the vanilla logout/quiescence story in the client README.
🦋 Changeset detectedLatest commit: 77d1f6c The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
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 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Bundle ReportChanges will increase total bundle size by 11.39kB (2.03%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: @knocklabs/client-client-cjsAssets Changed:
Files in
Files in
Files in
Files in
Files in
Files in
Files in
view changes for bundle: @knocklabs/react-core-react-core-esmAssets Changed:
Files in
Files in
Files in
Files in
Files in
Files in
Files in
view changes for bundle: @knocklabs/expo-expo-cjsAssets Changed:
Files in
view changes for bundle: @knocklabs/react-core-react-core-cjsAssets Changed:
Files in
Files in
Files in
Files in
Files in
Files in
Files in
view changes for bundle: @knocklabs/expo-expo-esmAssets Changed:
Files in
view changes for bundle: @knocklabs/client-client-esmAssets Changed:
Files in
Files in
Files in
Files in
Files in
Files in
Files in
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b9854aa. Configure here.
Replace the disposedRef/generation StrictMode self-heal with a plain effect cleanup that tears the client down on unmount. This removes the one-render window where a StrictMode simulated remount could return the previous (torn-down) memoized client before the generation bump rebuilt it. Addresses Cursor BugBot: "StrictMode serves torn-down client".
Wait for the connection status to resolve back to "connected" (which only happens after the second authCheck promise resolves) rather than asserting it synchronously right after waitFor(authCheck called twice), which was flaky under full-suite timing.
enabled prop to KnockProvider (SDK-wide quiescence + auth-state signal)enabled prop to KnockProvider
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #1033 +/- ##
==========================================
+ Coverage 64.49% 65.40% +0.91%
==========================================
Files 212 213 +1
Lines 10217 10473 +256
Branches 1389 1487 +98
==========================================
+ Hits 6589 6850 +261
+ Misses 3603 3598 -5
Partials 25 25
|

Description
Adds an
enabledprop to<KnockProvider>. When it'sfalse, children still render but the Knock client sits idle: no identify call, no API requests, no websocket. Set it totrueand it connects like a login; set it back tofalseand it shuts down like a logout. It defaults totrue, so existing code is unaffected.Today, if you don't have a user yet (common when the user token loads asynchronously), your only option is to conditionally mount the provider. This replaces that:
The prop itself is small. Most of the work is that a lot of the SDK used to throw, or fire requests with
userId=undefined, when no user was set. The worst case: rendering Guides before a user was set would throw and crash the app. This PR makes all of those paths do nothing when there's no user.Four commits, one per area:
@knocklabs/client— With no signed-in user, feed actions, Guides, and Slack/Teams checks now no-op instead of throwing or calling the API. Addsknock.logout()(disconnects the websocket, clears the token-refresh timer, removes listeners) andknock.authStatus/knock.authStoreso you can check or subscribe to whether a user is signed in. Also fixes two existing Guide bugs: real-time updates breaking after a re-login, and a location-tracking patch that broke when a Guide provider remounted.@knocklabs/react-core— Theenabledprop. When it's off, the provider passes no user, so the client stays logged out; toggling it rebuilds a fresh client so the feed reloads on login and clears out on logout. Also stopsuseFeedSettingsfrom calling/v1/users/undefined/..., and disconnects the client when the provider unmounts.react-core+expo— NewuseKnockAuthState()hook. Slack/Teams connection status now re-checks when the user changes (before, it checked once and stuck with that result). Expo waits for a signed-in user before registering for push, so logged-out users don't see the OS permission prompt.enabledprop.Changesets are included for every affected package. The full test suite passes (981 tests). Replaces #820, which was stale and had a bug where the feed never loaded after enabling.
Todos
enabledprop.Checklist