小编xxx*_*ope的帖子

多种策略的 Nestjs 护照认证

我有多种身份验证策略,例如其中之一:

@Injectable()
export class EmployeeStrategy extends PassportStrategy(Strategy, 'employee') {
  constructor(
    private authService: AuthService,
    @Inject(appConfig.KEY)
    configService: ConfigType<typeof appConfig>,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: configService.EMPLOYEE_KEY,
    });
  }

  async validate({ phone }: JwtPayload) {
    const employee = await this.authService.authByRole(phone, Role.Employee);

    if (!employee) {
      throw new UnauthorizedException('insufficient scope');
    }

    return employee;
  }
Run Code Online (Sandbox Code Playgroud)

还有一些人大多喜欢这个。但是因为我在其中抛出了未经授权的异常,所以我不能在同一个路由/控制器上使用多个异常。例如

  @UseGuards(AuthGuard(['employee', 'admin']))
Run Code Online (Sandbox Code Playgroud)

第一个崩溃导致错误的。如何解决这个问题?

nestjs

8
推荐指数
1
解决办法
1818
查看次数

SheetJS 如何从 json 创建工作表并将其保存为缓冲区

文档对我来说似乎很混乱。如何从对象数组创建文档,每个对象代表一行并将其保存为缓冲区?

xlsx node.js sheetjs

8
推荐指数
1
解决办法
6758
查看次数

Postgresql 函数返回整数或 null

我有一个这样的函数,它检查适当的帐户类型:

CREATE OR REPLACE FUNCTION acc_is_exclusive(acc_no integer, acc_type char) RETURNS integer AS $$
  BEGIN
    RETURN (SELECT 1 FROM account WHERE account_no = acc_no AND account_type = acc_type);
  END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

返回类型为整数,如果没有找到记录,则返回 NULL。这有道理吗?或者我需要以某种方式显式声明返回类型将是整数或空?

postgresql

4
推荐指数
1
解决办法
2156
查看次数

Template function to load big endian from byte array

I'm trying to implement template function, for reading from byte array in big endian order. This is my current implementation:

template<typename T>
T load_big_endian(const unsigned char* buf) {
  T res {};

  std::size_t size = sizeof(T) - 1;

  for (int i = 0; i <= size; i += 1) {
    res |= static_cast<T>(buf[size - i] << (i * 8));
  }

  return res;
}
Run Code Online (Sandbox Code Playgroud)

Is this a good solution? Or better use a separate functions for each type, like load32_big_endian? And …

c++ c++20

0
推荐指数
1
解决办法
65
查看次数

标签 统计

c++ ×1

c++20 ×1

nestjs ×1

node.js ×1

postgresql ×1

sheetjs ×1

xlsx ×1