Skip to content

Commit

Permalink
chore(nextjs): Drop redirect from RSC <Gate/>
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Nov 27, 2023
1 parent 26011dc commit 9dfe2b3
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions packages/nextjs/src/app-router/server/controlComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { experimental__CheckAuthorizationWithCustomPermissions } from '@clerk/types';
import { redirect } from 'next/navigation';
import React from 'react';

import { auth } from './auth';
Expand All @@ -17,43 +16,21 @@ export function SignedOut(props: React.PropsWithChildren) {
}

type GateServerComponentProps = React.PropsWithChildren<
Parameters<experimental__CheckAuthorizationWithCustomPermissions>[0] &
(
| {
fallback: React.ReactNode;
redirectTo?: never;
}
| {
fallback?: never;
redirectTo: string;
}
)
Parameters<experimental__CheckAuthorizationWithCustomPermissions>[0] & {
fallback?: React.ReactNode;
}
>;

/**
* @experimental The component is experimental and subject to change in future releases.
*/
export function experimental__Gate(gateProps: GateServerComponentProps) {
const { children, fallback, redirectTo, ...restAuthorizedParams } = gateProps;
const { children, fallback, ...restAuthorizedParams } = gateProps;
const { experimental__has } = auth();

const isAuthorizedUser = experimental__has(restAuthorizedParams);

if (!redirectTo && !fallback) {
throw new Error('Provide `<Gate />` with a `fallback` or `redirectTo`');
}

const handleFallback = () => {
if (redirectTo) {
return redirect(redirectTo);
}

return <>{fallback}</>;
};

if (!isAuthorizedUser) {
return handleFallback();
if (experimental__has(restAuthorizedParams)) {
return <>{children}</>;
}

return <>{children}</>;
return <>{fallback ?? null}</>;
}

0 comments on commit 9dfe2b3

Please sign in to comment.