1.0.0-rc.13 • Published 3 months ago

@exobase/use-role-authorization v1.0.0-rc.13

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

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
)
1.0.0-rc.13

3 months ago

1.0.0-rc.12

3 months ago

1.0.0-rc.11

3 months ago

1.0.0-rc.10

3 months ago

1.0.0-rc.8

1 year ago

1.0.0-rc.7

1 year ago

1.0.0-rc.5

1 year ago

1.0.0-rc.6

1 year ago

1.0.0-rc.3

1 year ago

1.0.0-rc.4

1 year ago

1.0.0-rc.2

1 year ago

1.0.0-rc.1

1 year ago