@exobase/use-role-authorization
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-rc.13 • Public • Published

title: 'useRoleAuthorization' description: 'A role authorization hook' group: 'Hooks' badge: 'Auth'

Exobase hook to check if a request is authorized given the roles that are attached to it.

Install

yarn add @exobase/use-role-authorization
# or
yarn add @exobase/hooks

Import

import { useRoleAuthorization } from '@exobase/use-role-authorization'
// or
import { useRoleAuthorization } from '@exobase/hooks'

Usage

import { compose } from 'radash'
import type { Props } from '@exobase/core'
import { useNext } from '@exobase/use-next'
import { useRoleAuthorization, useTokenAuth } from '@exobase/hooks'
import type { TokenAuth } from '@exobase/hooks'

type Args = {
  id: string
  price: number
}

type Services = {
  db: Database
}

export const updateListing = async ({
  args,
  services
}: Props<Args, Services, TokenAuth>) => {
  const { id, price } = args
  const { db } = services
  await db.listings.update(id, { price })
}

export default compose(
  useNext(),
  useJsonBody(z => ({
    id: z.string(),
    price: z.number()
  })),
  useTokenAuth(config.auth.tokens.secret),
  useRoleAuthorization<Props<Args, {}, TokenAuth>>({
    roles: ({ auth }) => auth.token.roles,
    require: 'admin'
  }),
  useServices<Services>({
    db: () => new Database()
  })
  updateListing
)

Readme

Keywords

none

Package Sidebar

Install

npm i @exobase/use-role-authorization

Weekly Downloads

10

Version

1.0.0-rc.13

License

MIT

Unpacked Size

15.1 kB

Total Files

17

Last publish

Collaborators

  • rayepps