NestJs 应用程序给了我 this._verify 在尝试实现自定义护照策略时不是一个函数。
我按照嵌套文档遵循了指导方针,并想知道我在实施中是否做错了什么。我能够实施 Local 和 JWT 策略,但无法实施。
以下是错误堆栈:
[Nest] 508 - 02/12/2022, 1:52:39 PM ERROR [ExceptionsHandler] this._verify is not a function
TypeError: this._verify is not a function
at LocalStratergy.Strategy.authenticate (D:\projects\capp\node_modules\passport-custom\lib\strategy.js:60:8)
at attempt (D:\projects\capp\node_modules\passport\lib\middleware\authenticate.js:369:16)
at authenticate (D:\projects\capp\node_modules\passport\lib\middleware\authenticate.js:370:7)
at D:\projects\capp\node_modules\@nestjs\passport\dist\auth.guard.js:91:3
at new Promise (<anonymous>)
at D:\projects\capp\node_modules\@nestjs\passport\dist\auth.guard.js:83:83
at LocalAuthGuard.<anonymous> (D:\projects\capp\node_modules\@nestjs\passport\dist\auth.guard.js:49:36)
at Generator.next (<anonymous>)
at fulfilled (D:\projects\capp\node_modules\@nestjs\passport\dist\auth.guard.js:17:58)
at runMicrotasks (<anonymous>)
以下是自定义策略代码:
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-custom';
@Injectable()
export class CustomStratergy extends PassportStrategy(Strategy) {
async validate(req: Request): Promise<any> {
const valid = true;
if (!valid) throw new UnauthorizedException();
return true;
}
}
这就是我在 NestJS 应用程序中注册它的方式:
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UserModule } from '../user/user.module';
import { PassportModule } from '@nestjs/passport';
import { CustomStratergy } from './stratergies/custom.stratergy';
import { JwtModule } from '@nestjs/jwt';
import { JwtStratergy } from './stratergies/jwt.stratergy';
@Module({
imports: [
UserModule,
PassportModule,
JwtModule.register({
secret: 'secret',
signOptions: { expiresIn: '1d' },
}),
],
providers: [AuthService, CustomStratergy, JwtStratergy],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {}
还有 AuthGuard:
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class CustomAuthGuard extends AuthGuard('custom') {}
我已经阅读了关于 SO 的其他类似错误文章,即使在关注它们之后也无法解决错误。因此,如果有人可以提供帮助,将不胜感激,谢谢!