You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
720 B

5 months ago
  1. import React from 'react'
  2. import { Props } from './types'
  3. import { useAtomValue } from 'jotai'
  4. import { authAtom } from './store/user.ts'
  5. export type AuthProps = Props & {
  6. //是否渲染没有权限的组件
  7. noAuth?: React.ReactNode
  8. //权限key
  9. authKey?: string[]
  10. }
  11. export const Auth: React.FC<AuthProps> = (props) => {
  12. const auth = useAtomValue(authAtom)
  13. if (!auth.isLogin) {
  14. return null
  15. }
  16. if (props.authKey && props.authKey.length > 0) {
  17. if (props.authKey.some(key => !auth.authKey?.includes(key))) {
  18. return props.noAuth || null
  19. }
  20. }
  21. return (
  22. <>
  23. {props!.children}
  24. </>
  25. )
  26. }
  27. export default Auth