Crud: Swagger Bearer Auth not cascading to request

Created on 4 May 2020  路  3Comments  路  Source: nestjsx/crud

I have this option in my main.js file

  const options = new DocumentBuilder()
    .setTitle('Adler')
    .setDescription('Style Quiz service for Boon')
    .setVersion(process.env.VERSION || '1.0')
    .addTag('Quiz')
    .addTag('Workspace')
    .addBearerAuth({ type: 'apiKey', name: 'Authorization', in: 'header' })
    .build();

this authorization header must be included in the request (if defined). This is working on my controllers not implementing the @Crud decorator but currently trying to implement this CRUD functionality on my new controllers. my problem is on the swagger document created by the app, the header wasn't passed onto this default methods created by the CRUD decorator attach to my controller.

Note: It is working when I use a postman request.

@UseGuards(AuthGuard('jwt'))
@Crud({
    model: {
        type: Workspace,
    },
})
@ApiTags('Workspace')
@Controller('workspace')
export class WorkspaceController implements CrudController<Workspace> {
    constructor(public service: WorkspaceService) {}
}

here's my example controller in which I have a JWT Strategy implemented thru @nestjs/passport in which it takes the header authorization from. here's the sample swagger requests demo: https://streamable.com/86dsbk

even appending thru @ApiHeader() doesn't work

@ApiHeader({
    name: 'Authorization'
})

Most helpful comment

You need to add @ApiBearerAuth() to your controller as well. This tells Swagger which endpoints get that Authorization: Bearer token in the request. You can have a mixture of protected and unprotected endponts.

All 3 comments

You need to add @ApiBearerAuth() to your controller as well. This tells Swagger which endpoints get that Authorization: Bearer token in the request. You can have a mixture of protected and unprotected endponts.

@rainercedric23 please let us know if @jeffreyschultz suggestion works for you

Thanks @jeffreyschultz it's working fine now when adding that decoration into my controller.

Was this page helpful?
0 / 5 - 0 ratings