Skip to content

Commit

Permalink
fix(nextjs,clerk-react,backend): Utility has is undefined when user…
Browse files Browse the repository at this point in the history
… is signed out
  • Loading branch information
panteliselef committed Dec 5, 2023
1 parent 41803e4 commit cf736cc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/backend/src/tokens/authObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createBackendApiClient } from '../api';
type AuthObjectDebugData = Record<string, any>;
type CreateAuthObjectDebug = (data?: AuthObjectDebugData) => AuthObjectDebug;
type AuthObjectDebug = () => AuthObjectDebugData;
type CheckAuthorizationSignedOut = undefined;

export type SignedInAuthObjectOptions = CreateBackendApiOptions & {
token: string;
Expand Down Expand Up @@ -52,7 +53,7 @@ export type SignedOutAuthObject = {
orgPermissions: null;
organization: null;
getToken: ServerGetToken;
has: CheckAuthorizationWithCustomPermissions;
has: CheckAuthorizationSignedOut;
debug: AuthObjectDebug;
};

Expand Down Expand Up @@ -126,7 +127,7 @@ export function signedOutAuthObject(debugData?: AuthObjectDebugData): SignedOutA
orgPermissions: null,
organization: null,
getToken: () => Promise.resolve(null),
has: () => false,
has: undefined,
debug: createDebug(debugData),
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/app-router/server/controlComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export function Protect(props: ProtectServerComponentProps) {
* Check against the results of `has` called inside the callback
*/
if (typeof restAuthorizedParams.condition === 'function') {
if (restAuthorizedParams.condition(has)) {
if (userId && restAuthorizedParams.condition(has)) {
return <>{children}</>;
}
return <>{fallback ?? null}</>;
}

if (has(restAuthorizedParams)) {
if (userId && has(restAuthorizedParams)) {
return <>{children}</>;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/controlComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ export const Protect = ({ children, fallback, ...restAuthorizedParams }: Protect
* Check against the results of `has` called inside the callback
*/
if (typeof restAuthorizedParams.condition === 'function') {
if (restAuthorizedParams.condition(has)) {
if (userId && restAuthorizedParams.condition(has)) {
return <>{children}</>;
}
return <>{fallback ?? null}</>;
}

if (has(restAuthorizedParams)) {
if (userId && has(restAuthorizedParams)) {
return <>{children}</>;
}

Expand Down
9 changes: 5 additions & 4 deletions packages/react/src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { invalidStateError, useAuthHasRequiresRoleOrPermission } from '../errors
import { errorThrower } from '../utils';
import { createGetToken, createSignOut } from './utils';

type CheckAuthorizationSignedOut = (params?: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;
type CheckAuthorizationSignedOut = undefined;
type CheckAuthorizationWithoutOrg = (params?: Parameters<CheckAuthorizationWithCustomPermissions>[0]) => false;

type UseAuthReturn =
| {
Expand Down Expand Up @@ -51,7 +52,7 @@ type UseAuthReturn =
orgId: null;
orgRole: null;
orgSlug: null;
has: CheckAuthorizationSignedOut;
has: CheckAuthorizationWithoutOrg;
signOut: SignOut;
getToken: GetToken;
}
Expand Down Expand Up @@ -147,7 +148,7 @@ export const useAuth: UseAuth = () => {
orgId: undefined,
orgRole: undefined,
orgSlug: undefined,
has: () => false,
has: undefined,
signOut,
getToken,
};
Expand All @@ -163,7 +164,7 @@ export const useAuth: UseAuth = () => {
orgId: null,
orgRole: null,
orgSlug: null,
has: () => false,
has: undefined,
signOut,
getToken,
};
Expand Down

0 comments on commit cf736cc

Please sign in to comment.