-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact: add PermissionsGate + debug state/log for forbidden errors
- Loading branch information
1 parent
f2f9265
commit 18a4f7c
Showing
12 changed files
with
322 additions
and
275 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
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 |
---|---|---|
@@ -1,17 +1,26 @@ | ||
import type { ReactNode } from "react"; | ||
import { useEffect } from "react"; | ||
import { Layout, Result } from "antd"; | ||
import { LAYOUT_CONTENT_STYLE } from "@/styles/layoutContent"; | ||
|
||
export type ForbiddenContentProps = { message?: ReactNode }; | ||
export type ForbiddenContentProps = { message?: ReactNode; debugState?: object }; | ||
|
||
const DEFAULT_MESSAGE = "You do not have permission to view this content."; | ||
|
||
const ForbiddenContent = ({ message }: ForbiddenContentProps) => ( | ||
<Layout> | ||
<Layout.Content style={LAYOUT_CONTENT_STYLE}> | ||
<Result status="error" title="Forbidden" subTitle={message ?? DEFAULT_MESSAGE} /> | ||
</Layout.Content> | ||
</Layout> | ||
); | ||
const ForbiddenContent = ({ message, debugState }: ForbiddenContentProps) => { | ||
useEffect(() => { | ||
if (debugState) { | ||
console.debug("ForbiddenContent debug state:", debugState); | ||
} | ||
}, [debugState]); | ||
|
||
return ( | ||
<Layout> | ||
<Layout.Content style={LAYOUT_CONTENT_STYLE}> | ||
<Result status="error" title="Forbidden" subTitle={message ?? DEFAULT_MESSAGE} /> | ||
</Layout.Content> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default ForbiddenContent; |
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,24 @@ | ||
import type { ReactNode } from "react"; | ||
import type { Resource } from "bento-auth-js"; | ||
import { useResourcePermissionsWrapper } from "@/hooks"; | ||
import ForbiddenContent from "@/components/ForbiddenContent"; | ||
|
||
type PermissionsGateProps = { | ||
resource: Resource; | ||
children: ReactNode; | ||
requiredPermissions: string[]; | ||
forbiddenMessage?: string; | ||
}; | ||
|
||
const PermissionsGate = ({ resource, children, requiredPermissions, forbiddenMessage }: PermissionsGateProps) => { | ||
const rp = useResourcePermissionsWrapper(resource); | ||
const { permissions, hasAttemptedPermissions } = rp; | ||
|
||
if (hasAttemptedPermissions && !requiredPermissions.every((reqPerm) => permissions.includes(reqPerm))) { | ||
return <ForbiddenContent message={forbiddenMessage} debugState={rp} />; | ||
} | ||
|
||
return children; | ||
}; | ||
|
||
export default PermissionsGate; |
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
Oops, something went wrong.