Skip to content

chore(types,clerk-react,astro,vue): Share Protect component prop type between SDKs #6197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/wet-foxes-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@clerk/astro": patch
"@clerk/clerk-react": patch
"@clerk/types": patch
"@clerk/vue": patch
---

Extract internal `ProtectProps` type to shared types to eliminate duplication across SDKs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ exports[`Typedoc output > should have a deliberate file structure 1`] = `
"types/path-value.mdx",
"types/pending-session-options.mdx",
"types/pending-session-resource.mdx",
"types/protect-props.mdx",
"types/record-to-path.mdx",
"types/redirect-options.mdx",
"types/reverification-config.mdx",
Expand Down
49 changes: 1 addition & 48 deletions packages/astro/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type {
Autocomplete,
CheckAuthorizationWithCustomPermissions,
Clerk,
ClerkOptions,
ClientResource,
MultiDomainAndOrProxyPrimitives,
OrganizationCustomPermissionKey,
OrganizationCustomRoleKey,
ProtectProps,
Without,
} from '@clerk/types';

Expand Down Expand Up @@ -48,50 +45,6 @@ declare global {
}
}

type ProtectProps =
| {
condition?: never;
role: OrganizationCustomRoleKey;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
feature?: never;
plan?: never;
permission: OrganizationCustomPermissionKey;
}
| {
condition: (has: CheckAuthorizationWithCustomPermissions) => boolean;
role?: never;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature: Autocomplete<`user:${string}` | `org:${string}`>;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan: Autocomplete<`user:${string}` | `org:${string}`>;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan?: never;
};

export type { AstroClerkUpdateOptions, AstroClerkIntegrationParams, AstroClerkCreateInstanceParams, ProtectProps };

export type ButtonProps<Tag> = {
Expand Down
54 changes: 2 additions & 52 deletions packages/react/src/components/controlComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { deprecated } from '@clerk/shared/deprecated';
import type {
Autocomplete,
CheckAuthorizationWithCustomPermissions,
HandleOAuthCallbackParams,
OrganizationCustomPermissionKey,
OrganizationCustomRoleKey,
PendingSessionOptions,
} from '@clerk/types';
import type { HandleOAuthCallbackParams, PendingSessionOptions, ProtectProps as _ProtectProps } from '@clerk/types';
import React from 'react';

import { useIsomorphicClerkContext } from '../contexts/IsomorphicClerkContext';
Expand Down Expand Up @@ -77,50 +70,7 @@ export const ClerkDegraded = ({ children }: React.PropsWithChildren<unknown>) =>
};

export type ProtectProps = React.PropsWithChildren<
(
| {
condition?: never;
role: OrganizationCustomRoleKey;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
feature?: never;
plan?: never;
permission: OrganizationCustomPermissionKey;
}
| {
condition: (has: CheckAuthorizationWithCustomPermissions) => boolean;
role?: never;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature: Autocomplete<`user:${string}` | `org:${string}`>;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan: Autocomplete<`user:${string}` | `org:${string}`>;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan?: never;
}
) & {
_ProtectProps & {
fallback?: React.ReactNode;
} & PendingSessionOptions
>;
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export * from './organizationSuggestion';
export * from './passwords';
export * from './permission';
export * from './phoneNumber';
export * from './protect';
export * from './redirects';
export * from './resource';
export * from './role';
Expand Down
70 changes: 70 additions & 0 deletions packages/types/src/protect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { OrganizationCustomPermissionKey, OrganizationCustomRoleKey } from './organizationMembership';
import type { CheckAuthorizationWithCustomPermissions } from './session';
import type { Autocomplete } from './utils';

/**
* Props for the `<Protect />` component, which restricts access to its children based on authentication and authorization.
*
* Use `ProtectProps` to specify the required role, permission, feature, or plan for access.
*
* @example
* ```tsx
* // Require a specific permission
* <Protect permission="a_permission_key" />
*
* // Require a specific role
* <Protect role="a_role_key" />
*
* // Use a custom condition callback
* <Protect condition={(has) => has({ permission: "a_permission_key" })} />
*
* // Require a specific feature
* <Protect feature="a_feature_key" />
*
* // Require a specific plan
* <Protect plan=a_plan_key" />
* ```
*/
export type ProtectProps =
| {
condition?: never;
role: OrganizationCustomRoleKey;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
feature?: never;
plan?: never;
permission: OrganizationCustomPermissionKey;
}
| {
condition: (has: CheckAuthorizationWithCustomPermissions) => boolean;
role?: never;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature: Autocomplete<`user:${string}` | `org:${string}`>;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan: Autocomplete<`user:${string}` | `org:${string}`>;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan?: never;
};
51 changes: 2 additions & 49 deletions packages/vue/src/components/controlComponents.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { deprecated } from '@clerk/shared/deprecated';
import type {
Autocomplete,
CheckAuthorizationWithCustomPermissions,
HandleOAuthCallbackParams,
OrganizationCustomPermissionKey,
OrganizationCustomRoleKey,
PendingSessionOptions,
ProtectProps as _ProtectProps,
RedirectOptions,
} from '@clerk/types';
import { defineComponent } from 'vue';
Expand Down Expand Up @@ -107,51 +104,7 @@ export const AuthenticateWithRedirectCallback = defineComponent((props: HandleOA
return () => null;
});

export type ProtectProps = (
| {
condition?: never;
role: OrganizationCustomRoleKey;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
feature?: never;
plan?: never;
permission: OrganizationCustomPermissionKey;
}
| {
condition: (has: CheckAuthorizationWithCustomPermissions) => boolean;
role?: never;
permission?: never;
feature?: never;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature: Autocomplete<`user:${string}` | `org:${string}`>;
plan?: never;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan: Autocomplete<`user:${string}` | `org:${string}`>;
}
| {
condition?: never;
role?: never;
permission?: never;
feature?: never;
plan?: never;
}
) &
PendingSessionOptions;
export type ProtectProps = _ProtectProps & PendingSessionOptions;

export const Protect = defineComponent((props: ProtectProps, { slots }) => {
const { isLoaded, has, userId } = useAuth({ treatPendingAsSignedOut: props.treatPendingAsSignedOut });
Expand Down