除一种方法外,如何对整个控制器应用授权

时间:2019-06-18 13:37:17

标签: node.js jwt

我在AuthGuard类上添加了UsersController,但是我想从授权中排除此控制器中的一种方法。

是否可以从授权中排除一种方法?

我在下面添加了我的代码:

@UseGuards(AuthGuard('jwt'))
@ApiUseTags('Users')
@Controller('users')
export class UsersController {
  constructor(public service: UsersService) {}
  get base(): CrudController<UsersService, UserEntity> {
    return this;
  }
  @Override()
  async createOne(@ParsedParams() params, @ParsedBody() body: UserEntity) {
    const username = body.username, email = body.email;
    const qb = await getRepository(UserEntity)
    .createQueryBuilder('users')
    .where('users.username = :username', { username })
    .orWhere('users.email = :email', { email });
    const _user = await qb.getOne();
    if (_user) {
      const error = { username: 'username already exists' };
      throw new HttpException({ message: 'Input data validation error', error }, HttpStatus.BAD_REQUEST);
    }
    return this.base.createOneBase(params, body);
  }
  @Post(':id/avatar')
  @UseInterceptors(FileInterceptor('file', {
    storage: multerDiskStorageConfig('users', 'avatar'),
  }))
  async uploadAvatarFile(@Param() params, @UploadedFile() file: any) {
    const userObject = await this.base.service.findOne();
    userObject.avatar = file.path.replace(PUBLIC_ROOT_PATH, '');
    return this.base.updateOneBase(params, userObject);
  }
}

0 个答案:

没有答案