Skip to content

Commit 82b84fe

Browse files
authored
fix(clerk-js, shared): Display empty data for authenticated billing hooks after sign out (#6747)
1 parent 7382e13 commit 82b84fe

File tree

8 files changed

+43
-12
lines changed

8 files changed

+43
-12
lines changed

.changeset/major-steaks-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/shared': patch
3+
---
4+
5+
Display empty data for authenticated billing hooks after sign out.

packages/shared/src/react/hooks/createCommerceHook.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ export function createCommercePaginatedHook<TResource extends ClerkResource, TPa
4545
options,
4646
}: CommerceHookConfig<TResource, TParams>) {
4747
type HookParams = PaginatedHookConfig<PagesOrInfiniteOptions> & {
48-
for: ForPayerType;
48+
for?: ForPayerType;
4949
};
5050

5151
return function useCommerceHook<T extends HookParams>(
5252
params?: T,
5353
): PaginatedResources<TResource, T extends { infinite: true } ? true : false> {
54-
const { for: _for, ...paginationParams } = params || ({ for: 'user' } as T);
54+
const { for: _for, ...paginationParams } = params || ({} as Partial<T>);
5555

5656
useAssertWrappedByClerkProvider(hookName);
5757

58-
const fetchFn = useFetcher(_for);
58+
const fetchFn = useFetcher(_for || 'user');
5959

6060
const safeValues = useWithSafeValues(paginationParams, {
6161
initialPage: 1,
@@ -83,14 +83,12 @@ export function createCommercePaginatedHook<TResource extends ClerkResource, TPa
8383
...(_for === 'organization' ? { orgId: organization?.id } : {}),
8484
} as TParams);
8585

86-
const isClerkLoaded = !!(clerk.loaded && (options?.unauthenticated ? true : user));
87-
8886
const isOrganization = _for === 'organization';
8987
const billingEnabled = isOrganization
9088
? environment?.commerceSettings.billing.organization.enabled
9189
: environment?.commerceSettings.billing.user.enabled;
9290

93-
const isEnabled = !!hookParams && isClerkLoaded && !!billingEnabled;
91+
const isEnabled = !!hookParams && clerk.loaded && !!billingEnabled;
9492

9593
const result = usePagesOrInfinite<TParams, ClerkPaginatedResponse<TResource>>(
9694
(hookParams || {}) as TParams,
@@ -99,6 +97,7 @@ export function createCommercePaginatedHook<TResource extends ClerkResource, TPa
9997
keepPreviousData: safeValues.keepPreviousData,
10098
infinite: safeValues.infinite,
10199
enabled: isEnabled,
100+
...(options?.unauthenticated ? {} : { isSignedIn: Boolean(user) }),
102101
__experimental_mode: safeValues.__experimental_mode,
103102
},
104103
{

packages/shared/src/react/hooks/usePagesOrInfinite.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export const usePagesOrInfinite: UsePagesOrInfinite = (params, fetcher, config,
148148
const cacheMode = config.__experimental_mode === 'cache';
149149
const triggerInfinite = config.infinite ?? false;
150150
const keepPreviousData = config.keepPreviousData ?? false;
151+
const isSignedIn = config.isSignedIn;
151152

152153
const pagesCacheKey = {
153154
...cacheKeys,
@@ -159,10 +160,13 @@ export const usePagesOrInfinite: UsePagesOrInfinite = (params, fetcher, config,
159160
// cacheMode being `true` indicates that the cache key is defined, but the fetcher is not.
160161
// This allows to ready the cache instead of firing a request.
161162
const shouldFetch = !triggerInfinite && enabled && (!cacheMode ? !!fetcher : true);
162-
const swrKey = shouldFetch ? pagesCacheKey : null;
163+
const swrKey = isSignedIn ? pagesCacheKey : shouldFetch ? pagesCacheKey : null;
163164
const swrFetcher =
164165
!cacheMode && !!fetcher
165166
? (cacheKeyParams: Record<string, unknown>) => {
167+
if (isSignedIn === false) {
168+
return null;
169+
}
166170
const requestParams = getDifferentKeys(cacheKeyParams, cacheKeys);
167171
return fetcher({ ...params, ...requestParams });
168172
}

packages/shared/src/react/hooks/usePaymentAttempts.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export const usePaymentAttempts = createCommercePaginatedHook<CommercePaymentRes
1111
resourceType: 'commerce-payment-attempts',
1212
useFetcher: () => {
1313
const clerk = useClerkInstanceContext();
14-
return clerk.billing.getPaymentAttempts;
14+
if (clerk.loaded) {
15+
return clerk.billing.getPaymentAttempts;
16+
}
17+
return undefined;
1518
},
1619
});

packages/shared/src/react/hooks/usePlans.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export const usePlans = createCommercePaginatedHook<CommercePlanResource, GetPla
1111
resourceType: 'commerce-plans',
1212
useFetcher: _for => {
1313
const clerk = useClerkInstanceContext();
14+
if (!clerk.loaded) {
15+
return undefined;
16+
}
1417
return ({ orgId, ...rest }) => {
1518
// Cleanup `orgId` from the params
1619
return clerk.billing.getPlans({ ...rest, for: _for });

packages/shared/src/react/hooks/useStatements.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export const useStatements = createCommercePaginatedHook<CommerceStatementResour
1111
resourceType: 'commerce-statements',
1212
useFetcher: () => {
1313
const clerk = useClerkInstanceContext();
14-
return clerk.billing.getStatements;
14+
if (clerk.loaded) {
15+
return clerk.billing.getStatements;
16+
}
17+
return undefined;
1518
},
1619
});

packages/shared/src/react/hooks/useSubscription.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ export const useSubscription = (params?: UseSubscriptionParams) => {
4747
: environment?.commerceSettings.billing.user.enabled;
4848

4949
const swr = useSWR(
50-
user?.id && billingEnabled
50+
billingEnabled
5151
? {
5252
type: 'commerce-subscription',
53-
userId: user.id,
53+
userId: user?.id,
5454
args: { orgId: isOrganization ? organization?.id : undefined },
5555
}
5656
: null,
57-
({ args }) => clerk.billing.getSubscription(args),
57+
({ args, userId }) => {
58+
// This allows for supporting keeping previous data between revalidations
59+
// but also hides the stale data on sign-out.
60+
if (userId) {
61+
return clerk.billing.getSubscription(args);
62+
}
63+
return null;
64+
},
5865
{
5966
dedupingInterval: 1_000 * 60,
6067
keepPreviousData: params?.keepPreviousData,

packages/shared/src/react/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ export type PagesOrInfiniteConfig = PaginatedHookConfig<{
118118
* @hidden
119119
*/
120120
__experimental_mode?: 'cache';
121+
122+
/**
123+
* @experimental
124+
*
125+
* @hidden
126+
*/
127+
isSignedIn?: boolean;
121128
}>;
122129

123130
/**

0 commit comments

Comments
 (0)