npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bty/react-auth

v0.0.13

Published

> 提供路由守卫和权限控制的 React 组件

Downloads

130

Readme

@bty/react-auth

提供路由守卫和权限控制的 React 组件

约定了 src/auth.ts 为我们的权限定义文件,该文件需要默认导出一个方法。该方法需要返回一个对象,对象的每一个值就对应定义了一条权限。如下所示:

使用

// src/auth.ts
import { FeatureFlag, type User } from './apis/user'

export interface AuthState {
  token?: string
  user: User
}

export default function (sate: AuthState) {
  return {
    auth: !!sate.token,
    canCreateWorkspace: sate.user?.featureFlag.includes(FeatureFlag.WORKSPACE_CREATION),
  }
}

页面权限控制

// router.ts
import { createAuthBrowserRouter } from '@bty/react-auth'
import auth from '@/auth'

const router = createAuthBrowserRouter([
  {
    path: '/',
    element: <>Home</>,
    auth: 'auth', // 该路由需要 auth.ts 中的 auth 字段为 true时 可访问

  }
], { auth })

export default router
// App.tsx
import { AuthProvider } from '@bty/react-auth'
import { RouterProvider } from 'react-router-dom'
import router from './router'
import { tokenStorage } from '@/utils/storage'
import { useUserStore } from '@/store'
import auth from '@/auth'

function App() {
  const { user } = useUserStore()
  return (
    <AuthProvider state={{ user, token: tokenStorage.get() }} authFn={auth} >{children}
      <RouterProvider router={router} />
    </AuthProvider>
  )
}

useAuth

import { useAuth } from '@bty/react-auth'

function Component() {
  const { access } = useAuth()
  return (
    <div>
      {access.auth && <div>登录了</div>}
      {access.canCreateWorkspace && <div>有创建空间权限</div>}
    </div>
  )
}

Access 组件(按钮权限)

import { Access, useAuth } from '@bty/react-auth'

function Component() {

  const { access } = useAuth()
  return (
    <Access access={access.canCreateWorkspace} callback={() => message.warning('您没有创建工作空间的权限,请联系管理员')} >
      <div>创建工作空间</div>
    </Access>
  )
}

Access 组件 Props

export interface AccessProps {
  children: React.ReactNode
  access: boolean // 是否有权限
  hide?: boolean // 无权限时是否隐藏按钮  默认 hide = false
  fallback?: React.ReactNode // 无权限时的回调内容
  callback?: () => void // 无权限时点击的回调内容
}