-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js,types): Introduce internal Gate component
- Loading branch information
1 parent
4c48269
commit da0696a
Showing
3 changed files
with
70 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { IsAuthorized } from '@clerk/types'; | ||
import type { ComponentType, PropsWithChildren, ReactNode } from 'react'; | ||
import React, { useEffect } from 'react'; | ||
|
||
import { useCoreSession } from '../contexts'; | ||
import { useFetch } from '../hooks'; | ||
import { useRouter } from '../router'; | ||
|
||
type GateParams = Parameters<IsAuthorized>[0]; | ||
type GateProps = PropsWithChildren< | ||
GateParams & { | ||
fallback?: ReactNode; | ||
redirectTo?: string; | ||
} | ||
>; | ||
|
||
export const useGate = (params: GateParams) => { | ||
const { isAuthorized } = useCoreSession(); | ||
const { data: isAuthorizedUser } = useFetch(isAuthorized, params); | ||
|
||
return { | ||
isAuthorizedUser, | ||
}; | ||
}; | ||
|
||
export const Gate = (gateProps: GateProps) => { | ||
const { children, fallback, redirectTo, ...restAuthorizedParams } = gateProps; | ||
|
||
const { isAuthorizedUser } = useGate(restAuthorizedParams); | ||
|
||
const { navigate } = useRouter(); | ||
|
||
useEffect(() => { | ||
// wait for promise to resolve | ||
if (typeof isAuthorizedUser === 'boolean' && !isAuthorizedUser && redirectTo) { | ||
void navigate(redirectTo); | ||
} | ||
}, [isAuthorizedUser, redirectTo]); | ||
|
||
// wait for promise to resolve | ||
if (typeof isAuthorizedUser === 'boolean' && !isAuthorizedUser && fallback) { | ||
return <>{fallback}</>; | ||
} | ||
|
||
if (isAuthorizedUser) { | ||
return <>{children}</>; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export function withGate<P>(Component: ComponentType<P>, gateProps: GateProps): React.ComponentType<P> { | ||
const displayName = Component.displayName || Component.name || 'Component'; | ||
const HOC = (props: P) => { | ||
return ( | ||
<Gate {...gateProps}> | ||
<Component {...(props as any)} /> | ||
</Gate> | ||
); | ||
}; | ||
|
||
HOC.displayName = `withGate(${displayName})`; | ||
|
||
return HOC; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters