【发布时间】:2020-10-21 12:25:01
【问题描述】:
我有多种身份验证策略,例如其中之一:
@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;
}
还有一些其他人大多喜欢这个。但是因为我在里面抛出了未经授权的异常,所以我不能在同一个路由/控制器上使用它们中的多个。例如
@UseGuards(AuthGuard(['employee', 'admin']))
第一个崩溃导致错误。如何解决这个问题?
【问题讨论】:
-
对不起,我不明白你的问题。你到底想做什么?
-
为什么在同一条路线上需要多个?只需创建不同的登录路径,例如
/login/employee和/login/admin -
可以分享
AuthGuard吗?
标签: nestjs