NodeJS NestJS未知身份验证策略“local”

5hcedyr0  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(154)

我在NestJs中有以下文件的代码,但现在它说“Unknown authentication strategy“local”",我已经寻找了解决方案,它们都指向导入错误,但我将localstrategy导入到auth.module和app.module中(我已经测试过将其从app.module中取出,但它不会改变任何东西。
app.module.ts

import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { ThrottlerModule } from '@nestjs/throttler';
    import { MongooseModule } from '@nestjs/mongoose';
    import { AuthModule } from './auth/auth.module';
    import { UsersModule } from './users/users.module';
    import { LocalStrategy } from './auth/local.strategy';

    @Module({
      imports: [
        MongooseModule.forRoot(
          'mongodb+srv://user:password@db.db.mongodb.net/db?retryWrites=true&w=majority',
        ),
        ThrottlerModule.forRoot({
          ttl: 60,
          limit: 10,
        }),
        AuthModule,
        UsersModule,
      ],
      controllers: [AppController],
      providers: [AppService, LocalStrategy],
    })
    export class AppModule {}

auth.module.ts

import { Module } from '@nestjs/common';
  import { AuthService } from './auth.service';
  import { UsersModule } from '../users/users.module';
  import { PassportModule } from '@nestjs/passport';
  import { LocalStrategy } from './local.strategy';
  import { JwtModule } from '@nestjs/jwt';
  import { JwtStrategy } from './jwt.strategy';
  import 'dotenv/config';

  @Module({
    imports: [
      UsersModule,
      PassportModule,
      JwtModule.register({
        secret: process.env.JWTSECRET,
        signOptions: { expiresIn: '60s' },
      }),
    ],
    providers: [AuthService, LocalStrategy, JwtStrategy],
    exports: [AuthService],
  })
  export class AuthModule {}

local.strategy.ts

import { Strategy } from 'passport-local';
  import { PassportStrategy } from '@nestjs/passport';
  import { Injectable, UnauthorizedException } from '@nestjs/common';
  import { AuthService } from './auth.service';

  @Injectable()
  export class LocalStrategy extends PassportStrategy(Strategy) {
    constructor(private authService: AuthService) {
      super({ usernameField: 'email' });
    }

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

app.controller.ts

import {
  Controller,
  Request,
  Post,
  UseGuards,
  Res,
  Get,
  Body,
} from '@nestjs/common';
import { AuthService } from './auth/auth.service';
import { MakeAuthDto } from './auth/dto/make-auth.dto';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { LocalAuthGuard } from './auth/local-auth.guard';
import { Roles } from './utils/decorators/roles.decorator';
import { Role } from './utils/enums/role.enum';
import { RolesGuard } from './utils/guards/roles.guard';

@Controller()
export class AppController {
  constructor(private authService: AuthService) {}

  @UseGuards(LocalAuthGuard)
  @Post('auth/login')
  async login(
    @Body() _: MakeAuthDto,
    @Request() req,
    @Res({ passthrough: true }) res,
  ) {
    console.log(req.user);
    const access_token = await this.authService.login(req.user);
    res.cookie('jwt', access_token);
    return req.user;
  }

  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.Admin)
  @Get('tests')
  getProfile(@Request() req) {
    return req.user;
  }
}

local-auth.guard.ts

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

    @Injectable()
    export class LocalAuthGuard extends AuthGuard('local') {}

auth.service.ts

import { Injectable } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';
import { JwtService } from '@nestjs/jwt';
import { UserDocument } from 'src/users/entities/user.entity';

@Injectable()
export class AuthService {
  constructor(
    private usersService: UsersService,
    private jwtService: JwtService,
  ) {}

  async validateUser(email: string, pass: string): Promise<UserDocument | any> {
    const user = await this.usersService.findOne(email);
    if (user && (await user.compareHash(pass))) {
      const { password, ...result } = user.toObject();
      await this.usersService.updateLastLogin(user._id);
      return result;
    }
    return null;
  }

  async login(user: UserDocument): Promise<string> {
    const payload = { email: user.email, sub: user._id, roles: user.roles };
    return this.jwtService.sign(payload);
  }
}

jwt-auth.guard.ts

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

    @Injectable()
    export class JwtAuthGuard extends AuthGuard('jwt') {}

jwt.strategy.ts

import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import 'dotenv/config';

const cookieExtractor = function (req) {
  let token = null;
  if (req && req.cookies) {
    token = req.cookies['jwt'];
  }
  return token;
};

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromExtractors([cookieExtractor]),
      ignoreExpiration: false,
      secretOrKey: process.env.JWTSECRET,
    });
  }

  async validate(payload: any) {
    return { userId: payload.sub, email: payload.email, roles: payload.roles };
  }
}

stackoverflow上没有类似的问题解决了我的问题,有人知道它可能是什么吗?

byqmnocz

byqmnocz1#

最近,我结束了面对这个错误,由于我的组织文件夹,它似乎没有找到文件“本地.战略”和“jwt.战略”,在每个后卫,我不得不添加一个构造函数导入战略.
现在你的代码是这样的:

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

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {}

通过添加如下构造来更改您的文件:

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

import { LocalStrategy } from './local.strategy';

@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
  constructor(private readonly localStrategy: LocalStrategy) {
    super();
  }
}

希望这对任何寻找此错误的人都有帮助!

相关问题