Skip to content

[Docs] bouncer cannot be spread from ctx #27

@fugudesign

Description

@fugudesign

Hi,

In the "Adding Edit Functionality" (https://docs.adonisjs.com/tutorial/react/authorization#adding-edit-functionality), when i follow this code block:

  @inject()
  async forDetailedView({ bouncer }: HttpContext) {
    return {
      ...this.toObject(),
      can: {
        edit: await bouncer.with(PostPolicy).allows('edit', this.resource),
        delete: await bouncer.with(PostPolicy).allows('delete', this.resource),
      },
    }
  }

it's produce this in my front console:

RuntimeException: Cannot inject "[Function: Object]" in "[class PostTransformer]"
Hint: The value is not a valid class
...

So, this workaround works for me, with bouncer non-spread:

@inject()
  async forDetailedView(ctx: HttpContext) {
    return {
      ...this.toObject(),
      can: {
        edit: await ctx.bouncer.with(PostPolicy).allows('edit', this.resource),
        delete: await ctx.bouncer.with(PostPolicy).allows('delete', this.resource),
      },
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions