我正在尝试实施护照本地策略,但验证方法不起作用。当我这样做时@UseGuards(AuthGuard("local")),它会自动抛出未经授权的异常,而无需通过我编写的验证方法。我不知道我做错了什么,因为文档也做了同样的事情。
这是我的LocalStrategy课程:
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(
@InjectRepository(UserRepository) private userRepository: UserRepository,
) {
super();
}
async validate(credentials: string, password: string): Promise<User> {
// this method is never called, I've already did some console.logs
const user = await this.userRepository.findByCredentials(credentials);
if (!user) throw new UnauthorizedException('Invalid credentials');
if (!(await argon2.verify(user.hash, password)))
throw new UnauthorizedException('Invalid credentials');
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
我的AuthModule导入:
@Module({
imports: [TypeOrmModule.forFeature([UserRepository]), PassportModule],
controllers: [AuthController],
providers: [AuthService, LocalStrategy],
})
export class AuthModule {} …Run Code Online (Sandbox Code Playgroud) authentication passport-local passport.js nestjs nestjs-passport