Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 470
Introduce internal Gate component#1834
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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. |
| 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. |
| 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; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,11 +24,11 @@ export const useFetch = <T>( | ||
| requestStatus.setLoading(); | ||
| fetcherRef | ||
| .current(params) | ||
| .then(domain => { | ||
| .then(result => { | ||
| requestStatus.setIdle(); | ||
| if (domain) { | ||
| setData({ ...domain }); | ||
| callbacks?.onSuccess?.({ ...domain }); | ||
| if (typeof result !== 'undefined') { | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for reviewer: if value is an object then do a shallow copy otherwise use the actual value | ||
| setData(typeof result === 'object' ? { ...result } : result); | ||
| callbacks?.onSuccess?.(typeof result === 'object' ? { ...result } : result); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: adding
(string & {})allows for getting eslint autocomplete but also accepts any stringThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we write this in a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call