如何排除nest.js中的路由验证



// jwt guard
@UseGuards(AuthStrategyGuard)
@Controller('users')
export class UsersController {
constructor(private readonly authService: AuthService, private readonly usersService: UsersService) {}
@Post()
async getIndex (@Body() body) {
if (!body.user) {
throw new UnauthorizedException('error auth');
} else {
const token = await this.authService.sign(body);
return { token }
}
}
@Post('auth')
validating (@Body('token') token) {
console.log(token, 'token');
return {success: 1}
}
@Post('test')
getTestIndex () {
return {success: 1}
}
}

这是JSONWebToken验证逻辑

我想从JWT验证中排除@post ()装饰器

我该怎么办

您可以通过仅在需要验证的方法(端点(上应用保护来实现这一点,在您的情况下,它将是:(我假设您希望从第一个路由中排除验证

@Controller('users')
export class UsersController {
constructor(private readonly authService: AuthService, private readonly usersService: UsersService) {}

@Post()
async getIndex (@Body() body) {
if (!body.user) {
throw new UnauthorizedException('error auth');
} else {
const token = await this.authService.sign(body);
return { token }
}
}
@UseGuards(AuthStrategyGuard)
@Post('auth')
validating (@Body('token') token) {
console.log(token, 'token');
return {success: 1}
}
@UseGuards(AuthStrategyGuard)
@Post('test')
getTestIndex () {
return {success: 1}
}
}

最新更新