Skip to content

Commit cf736cc

Browse files
committed
fix(nextjs,clerk-react,backend): Utility has is undefined when user is signed out
1 parent 41803e4 commit cf736cc

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

packages/backend/src/tokens/authObjects.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { createBackendApiClient } from '../api';
1414
type AuthObjectDebugData = Record<string, any>;
1515
type CreateAuthObjectDebug = (data?: AuthObjectDebugData) => AuthObjectDebug;
1616
type AuthObjectDebug = () => AuthObjectDebugData;
17+
type CheckAuthorizationSignedOut = undefined;
1718

1819
export type SignedInAuthObjectOptions = CreateBackendApiOptions & {
1920
token: string;
@@ -52,7 +53,7 @@ export type SignedOutAuthObject = {
5253
orgPermissions: null;
5354
organization: null;
5455
getToken: ServerGetToken;
55-
has: CheckAuthorizationWithCustomPermissions;
56+
has: CheckAuthorizationSignedOut;
5657
debug: AuthObjectDebug;
5758
};
5859

@@ -126,7 +127,7 @@ export function signedOutAuthObject(debugData?: AuthObjectDebugData): SignedOutA
126127
orgPermissions: null,
127128
organization: null,
128129
getToken: () => Promise.resolve(null),
129-
has: () => false,
130+
has: undefined,
130131
debug: createDebug(debugData),
131132
};
132133
}

packages/nextjs/src/app-router/server/controlComponents.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ export function Protect(props: ProtectServerComponentProps) {
4747
* Check against the results of `has` called inside the callback
4848
*/
4949
if (typeof restAuthorizedParams.condition === 'function') {
50-
if (restAuthorizedParams.condition(has)) {
50+
if (userId && restAuthorizedParams.condition(has)) {
5151
return <>{children}</>;
5252
}
5353
return <>{fallback ?? null}</>;
5454
}
5555

56-
if (has(restAuthorizedParams)) {
56+
if (userId && has(restAuthorizedParams)) {
5757
return <>{children}</>;
5858
}
5959

packages/react/src/components/controlComponents.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ export const Protect = ({ children, fallback, ...restAuthorizedParams }: Protect
102102
* Check against the results of `has` called inside the callback
103103
*/
104104
if (typeof restAuthorizedParams.condition === 'function') {
105-
if (restAuthorizedParams.condition(has)) {
105+
if (userId && restAuthorizedParams.condition(has)) {
106106
return <>{children}</>;
107107
}
108108
return <>{fallback ?? null}</>;
109109
}
110110

111-
if (has(restAuthorizedParams)) {
111+
if (userId && has(restAuthorizedParams)) {
112112
return <>{children}</>;
113113
}
114114

packages/react/src/hooks/useAuth.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { invalidStateError, useAuthHasRequiresRoleOrPermission } from '../errors
1313
import { errorThrower } from '../utils';
1414
import { createGetToken, createSignOut } from './utils';
1515

16-
type CheckAuthorizationSignedOut = (params?: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;
16+
type CheckAuthorizationSignedOut = undefined;
17+
type CheckAuthorizationWithoutOrg = (params?: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;
1718

1819
type UseAuthReturn =
1920
| {
@@ -51,7 +52,7 @@ type UseAuthReturn =
5152
orgId: null;
5253
orgRole: null;
5354
orgSlug: null;
54-
has: CheckAuthorizationSignedOut;
55+
has: CheckAuthorizationWithoutOrg;
5556
signOut: SignOut;
5657
getToken: GetToken;
5758
}
@@ -147,7 +148,7 @@ export const useAuth: UseAuth = () => {
147148
orgId: undefined,
148149
orgRole: undefined,
149150
orgSlug: undefined,
150-
has: () => false,
151+
has: undefined,
151152
signOut,
152153
getToken,
153154
};
@@ -163,7 +164,7 @@ export const useAuth: UseAuth = () => {
163164
orgId: null,
164165
orgRole: null,
165166
orgSlug: null,
166-
has: () => false,
167+
has: undefined,
167168
signOut,
168169
getToken,
169170
};

0 commit comments

Comments
 (0)