具有多种策略的Nestjs passport身份验证

具有多种策略的Nestjs passport身份验证,nestjs,Nestjs,我有多种身份验证策略,例如: @Injectable() 导出类EmployeeStrategy扩展了PassportStrategy(策略“employee”){ 建造师( 私有authService:authService, @注入(appConfig.KEY) configService:ConfigType, ) { 超级({ jwtFromRequest:ExtractJwt.FromAuthHeaderAsberToken(), secretrokey:configService.E

我有多种身份验证策略,例如:

@Injectable()
导出类EmployeeStrategy扩展了PassportStrategy(策略“employee”){
建造师(
私有authService:authService,
@注入(appConfig.KEY)
configService:ConfigType,
) {
超级({
jwtFromRequest:ExtractJwt.FromAuthHeaderAsberToken(),
secretrokey:configService.EMPLOYEE\u密钥,
});
}
异步验证({phone}:JwtPayload){
const employee=wait this.authService.authByRole(电话,角色.employee);
如果(!雇员){
抛出新的UnauthorizedException(“范围不足”);
}
返回员工;
}
还有一些人更喜欢这个。但是因为我在其中抛出了未经授权的异常,所以我不能在同一个路由/控制器上使用多个异常

  @UseGuards(AuthGuard(['employee', 'admin']))

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

@xxx\u coder\u noscope您对该策略的看法有点错误。这里的策略是一种如何从定义的位置(HTTP头、查询、参数、cookies等)获取特殊令牌、密钥等的方法。
validate()返回的实体
方法将作为
用户
属性注入请求对象


稍后通过创建
EmployeeGuard
作为
EmployeeGuard实现canActivate
并覆盖
canActivate()
方法按类型检查用户角色,并返回布尔值以允许或拒绝访问端点

对不起,我不明白你的问题。你到底想做什么?为什么在同一路径上需要多个?只需创建不同的登录路径,如
/login/employee
/login/admin
你能共享
AuthGu吗ard