目前,我已經(jīng)設(shè)法將AuthGuard與JWT身份驗(yàn)證結(jié)合使用,但是我沒有設(shè)法繼續(xù)進(jìn)行user下去Roles。我嘗試遵循cats示例,但從user未定義對象,您可以在第14行看到。這是我的代碼:// auth.controller.ts@Controller('auth')@UseGuards(RolesGuard)export class AuthController { constructor(private readonly authService: AuthService) {} @Get('token') async createToken(): Promise<any> { return await this.authService.createToken(); } @Post() @UseGuards(AuthGuard()) @Roles('admin') findAll() { // this route is restricted by AuthGuard // JWT strategy return 'Super important info'; }}// auth.module.ts@Module({ imports: [ SharedModule, PassportModule.register({ defaultStrategy: 'jwt' }), JwtModule.registerAsync({ imports: [SharedModule], useFactory: async (configService: ConfigService) => ({ secretOrPrivateKey: configService.get('SECRET_KEY'), signOptions: { expiresIn: configService.get('SECRET_KEY_EXPIRES'), }, }), inject: [ConfigService], }), ], controllers: [AuthController], providers: [ AuthService, JwtStrategy, ],})export class AuthModule {}其余所有內(nèi)容均與存儲庫中的示例完全相同。任何想法如何能夠得到users定義?
NestJS-如何在JWT中使用RoleGuard?
動漫人物
2021-04-10 18:15:29