【问题标题】:NestJS - Error: Unknown authentication strategy "local"NestJS - 错误:未知的身份验证策略“本地”
【发布时间】: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 的导入中。

标签: javascript authentication passport.js nestjs passport-local


【解决方案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 你有解决办法吗?我也遇到了同样的问题,不知道怎么解决。
猜你喜欢
  • 2022-07-19
  • 2022-01-11
  • 2014-02-22
  • 1970-01-01
  • 2018-06-08
  • 2017-05-25
  • 2016-02-28
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多