Skip to content

Commit

Permalink
fix(tsdoc): tsdoc added for ComponentShield
Browse files Browse the repository at this point in the history
  • Loading branch information
imjulianeral committed Sep 11, 2021
1 parent 62ec048 commit 7f6bf52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
NextShield
</h1>

😉 The shield that every Next.js project needs.
😉 The shield that every Next.js app needs.

## Motivation

Expand All @@ -27,6 +27,6 @@ After creating a lot of apps with the same boilerplate code for authentication &

You can find the documentation at the [official website](https://imjulianeral.github.io/next-shield/).

## Examples
<!-- ## Examples
[Firebase Example](https://github.com/imjulianeral/next-shield/tree/main/example/ts-fire-example)
[Firebase Example](https://github.com/imjulianeral/next-shield/tree/main/example/ts-fire-example) -->
38 changes: 33 additions & 5 deletions src/components/ComponentShield.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,43 @@ import React from 'react'
import {
ComponentShieldAuthProps,
ComponentShieldBaseProps,
ComponentShieldProps,
ComponentShieldRBACProps,
ComponentShieldProps,
} from '../types/Component'

/**
* 🛡️ A Shield for your Components, it handles when a component shows or not based on the auth state or the user's role
*
* @param ComponentShieldProps {@link ComponentShieldProps | see here}
* @returns Children Components | {@link JSX.Element}
*
*
* @example
* ```tsx
* // Basic Example
* <ComponentShield showIf={true}>
* <p>You are authorized</p>
* </ComponentShield>
*
* // Fallback Example
* <ComponentShield showIf={false} fallback={<p>You are unauthorized</p>}>
* <p>You are authorized</p>
* </ComponentShield>
*
* // RBAC Example
* <ComponentShield RBAC showForRole="ADMIN" userRole="ADMIN">
* <p>You are an ADMIN</p>
* </ComponentShield>
* ```
*
* @packageDocumentation
*/

export function ComponentShield(props: ComponentShieldBaseProps): JSX.Element
export function ComponentShield(props: ComponentShieldAuthProps): JSX.Element
export function ComponentShield(props: ComponentShieldRBACProps): JSX.Element
export function ComponentShield(props: ComponentShieldProps) {
const { showForRole, showIf, fallback, RBAC, userRole, children } = props
const { showForRole, showIf, fallback = null, RBAC, userRole, children } = props

if (RBAC) return <>{showForRole === userRole ? children : null}</>
if (showIf) return <>{children}</>
Expand All @@ -22,9 +50,9 @@ export function ComponentShield(props: ComponentShieldProps) {
// function Debug() {
// return (
// <>
// <ComponentShield showIf={true}>
// <p>Default</p>
// </ComponentShield>
// <ComponentShield showIf={true}>
// <p>Default</p>
// </ComponentShield>
// <ComponentShield RBAC showForRole="" userRole="">
// <p>RBAC</p>
// </ComponentShield>
Expand Down

0 comments on commit 7f6bf52

Please sign in to comment.