-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce internal Gate component (#1834)
* feat(clerk-js,types): Introduce Session.isAuthorized * feat(clerk-js): Introduce internal Gate component
- Loading branch information
1 parent
7f4d4b9
commit 997b8e2
Showing
13 changed files
with
219 additions
and
9 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,6 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
Introduces a new `isAuthorized()` method in the `Session` class. Returns a promise and checks whether the active user is allowed to perform an action based on the passed (required) permission and the ones attached to the membership. |
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,5 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
--- | ||
|
||
Introduces an internal `<Gate/>` component (supporting hook and HOC) which enables us to conditionally render parts of our components based on a users permissions. |
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
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
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
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
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
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