NestJS - 错误:未知的身份验证策略“本地”

Posted

技术标签:

【中文标题】NestJS - 错误:未知的身份验证策略“本地”【英文标题】:NestJS - Error: Unknown authentication strategy "local" 【发布时间】:2021-12-15 16:38:13 【问题描述】:

这些是我拥有的部分 NestJS 代码 sn-ps。我正在尝试实施护照本地策略来获取用户名和密码。我收到 -Error: Unknown authentication strategy "local", in the controller file when using the auth guard.

AuthModule.ts

import  Module  from '@nestjs/common';
import  JwtModule  from '@nestjs/jwt';
import  PassportModule  from '@nestjs/passport';
import  UserModule  from 'src/user/user.module';
import  JwtAuthController  from './jwt-auth.controller';
import  JwtAuthService  from './jwt-auth.service';
import  JwtStrategy  from './jwt.strategy';
import  LocalStrategy  from './local.strategy';

@Module(
  imports: [
    UserModule,
    PassportModule,
    JwtModule.register(
      secret: process.env.SECRETKEY,
      signOptions:  expiresIn: '3600s' 
    )
  ],
  controllers: [JwtAuthController],
  providers: [JwtAuthService, LocalStrategy, JwtStrategy],
  exports: [JwtAuthService],
)
export class JwtAuthModule 

local.strategy.ts

import  Strategy  from 'passport-local';
import  PassportStrategy  from '@nestjs/passport';
import  Injectable, UnauthorizedException  from '@nestjs/common';
import  JwtAuthService  from './jwt-auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, 'local') 
  constructor(private authService: JwtAuthService) 
    super();
  

  async validate(username: string, password: string): Promise<any> 
    const user = await this.authService.validateUser(username, password);
    if (!user) 
      throw new UnauthorizedException();
    
    return user;
  

app.controller.ts

import  Body, Controller, Get, Post, Req, UnauthorizedException, UseGuards  from '@nestjs/common';
import  AuthGuard  from '@nestjs/passport';
import  JwtAuthService  from './jwt-auth/jwt-auth.service';

@Controller()
export class AppController 

  constructor(private readonly authService: JwtAuthService)  
  
  @UseGuards(AuthGuard('local'))
  @Post('/auth/login')
  async login(@Req() req) 
    return this.authService.login(req.user)
  
  

我在调用 /auth/login API 时收到以下错误

[Nest] 26753  - 10/31/2021, 22:08:18   ERROR [ExceptionsHandler] Unknown authentication strategy "local"
Error: Unknown authentication strategy "local"

我错过了什么吗?提前致谢。

【问题讨论】:

AppController 生活在哪个模块中? AppController 在 app.module.ts 中。我已经从 auth.module 导出了 auth.service.ts 并且 auth.module 被导入到了 app.module.ts 你的 JwtAuthService REQUEST 是作用域吗? 我是 NESTjs 的新手。如何确定 JwtAuthService REQUEST 是否有范围? 尝试将AuthModule 添加到app.module.ts 的导入中。 【参考方案1】:

修复错误https://github.com/nestjs/nest/issues/4646

文件 AUTH.ts

enter code here

import  Module  from '@nestjs/common';
import  JwtModule  from '@nestjs/jwt';
import  PassportModule  from '@nestjs/passport';
import  UserModule  from 'src/user/user.module';
import  JwtAuthController  from './jwt-auth.controller';
import  JwtAuthService  from './jwt-auth.service';
import  JwtStrategy  from './jwt.strategy';
import  LocalStrategy  from './local.strategy';

@Module(
imports: [
UserModule,
PassportModule.register(defaultStrategy:'local'),
JwtModule.register(
  secret: process.env.SECRETKEY,
  signOptions:  expiresIn: '3600s' 
)
],
controllers: [JwtAuthController],
providers: [JwtAuthService, LocalStrategy, JwtStrategy],
exports: [JwtAuthService],
)
export class JwtAuthModule 

【讨论】:

我尝试添加 defaultStrategy,但仍然无法正常工作。我遇到了同样的错误。 @adhinarayan 你有解决办法吗?我也遇到了同样的问题,不知道怎么解决。

以上是关于NestJS - 错误:未知的身份验证策略“本地”的主要内容,如果未能解决你的问题,请参考以下文章

节点护照错误:未知身份验证策略“本地登录”

护照 未知的身份验证策略“本地”、“脸书”、“谷歌”

Passport:未知的身份验证策略“本地”

得到“错误”:“未知的身份验证策略\”jwt\“”

passport.js 抛出未知的身份验证策略“谷歌”

登陆失败:未知的用户名或密码错误解决办法