【发布时间】:2019-07-01 13:14:36
【问题描述】:
我正在使用 nestjs,但在使用守卫对请求进行身份验证时遇到问题。
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException, HttpStatus, Logger } from '@nestjs/common';
import { Strategy } from 'passport-localapikey-update';
import { size } from 'lodash';
import { AuthService } from './auth.service';
@Injectable()
export class ApiKeyStrategy extends PassportStrategy(Strategy, 'localapikey') {
constructor(private readonly authService: AuthService) {
super();
}
async validate(token: string) {
Logger.log('HERE!!!!!!!!!!!!!!', 'ApiKeyStrategy'); // Not printed
const data = await this.authService.authenticateClient(token);
if (!size(data)) {
throw new UnauthorizedException('Unauthorized');
}
return data;
}
}
@UseGuards(AuthGuard('localapikey')) 不执行并抛出 401 错误。
不打印任何日志。
【问题讨论】:
标签: javascript node.js typescript passport.js nestjs