首页 > 解决方案 > NestJS 8 自己的 AuthGuard 和 Passport:无法读取未定义的属性“身份验证”

问题描述

我有自己的 AuthGuard 实现:

import { authenticate } from 'passport';
@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(context: ExecutionContext){
    ...
    authenticate(...); 
  }
}

当我调用authenticate方法时,我得到:Cannot read property 'authenticate' of undefined at Authenticator.authenticate (.../node_modules/passport/lib/authenticator.js:162:26

好像 Passport 还没有启动。

授权模块:

@Module({
  imports: [PassportModule],
  providers: [
    AuthGuard,
    {
      provide: APP_GUARD,
      useClass: AuthGuard,
    },
  ],
})
export class AuthModule {}

和应用模块:

@Module({
  imports: [
    AuthModule
    ]    
 })
export class AppModule {}

这段代码在 Nest 7 中运行。

你知道有什么问题吗?

标签: passport.jsnestjsnestjs-passport

解决方案


推荐阅读