NodeJS 此._verify不是NestJS自定义策略上的函数

j5fpnvbx  于 2023-03-17  发布在  Node.js
关注(0)|答案(1)|浏览(88)

NestJs应用程序在尝试实现自定义passport策略时向我提供了._verify不是一个函数。
我遵循了nest文档中的指导方针,并想知道我在实现中是否做错了什么。我能够实现本地和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 {}

此外,身份验证防护:

import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class CustomAuthGuard extends AuthGuard('custom') {}

我已经通过了其他类似的错误文章的SO和无法解决的错误,即使在以下他们。所以,如果任何人可以帮助它将不胜感激,谢谢!

clj7thdc

clj7thdc1#

我遇到了同样的问题。在我的例子中,我有一个构造函数,并且我向super提供了一个不必要的配置。这个问题通过简单地调用super()而得到了解决,没有任何参数。
为了方便起见,您可以尝试添加一个空构造函数吗?

constructor() {
    super()
}

相关问题