使用本地护照进行 Nest.js 身份验证导致错误:未知身份验证策略“本地”

问题描述 投票:0回答:1

我是 Nest.js 的新手,目前正在尝试弄清楚如何让我的身份验证运行。我使用 passport-local 并按照官方 Nest.js 网站上的教程进行操作,但我一直收到错误

Unknown authentication strategy "local"
。这是我的代码,因为我没有什么可说的(我留下了一些服务,如用户服务,因为我认为它们与问题没有任何关系,但我可以根据要求发送它们)。这是我的“第一个”真正的问题(几年前我小时候问过一些问题)。我希望这不是太多的代码,但我不知道如何说明我的问题。我希望这没关系。谢谢你的帮助! :)

auth.module.ts:

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

@Module({ 
  imports: [UsersModule, PassportModule],
  providers: [AuthService, LocalStrategy]
})
export class AuthModule {}

local-auth.guard.ts:

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

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

local.strategy.ts:

import { Injectable } from "@angular/core"
import { UnauthorizedException } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport"
import { Strategy } from "passport-local"
import { AuthService } from "./auth.service"
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
    constructor(private authService: AuthService) {
        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.module.ts:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

app.controller.ts

import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common';
import { LocalAuthGuard } from '../auth/local-auth.guard';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @UseGuards(LocalAuthGuard)
  @Post('login')
  login(@Request() req): any {
    return req.user;
  }

  @Get('protected')
  getData() {
    return this.appService.getData();
  }
}

auth.service.ts

import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';

@Injectable()
export class AuthService {

    constructor (private userService: UsersService) {}

    async validateUser(username: string, password: string): Promise<any> {
        const user = await this.userService.existsUser(username);
        if (user && (user.password === password)) {
            const {password, username, ...rest} = user; 
            return rest;
        }
        return null;
    } 
}

user.service.ts

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

export type User = {
    id: number,
    name: string,
    username: string,
    password: string
}

@Injectable()
export class UsersService {
    private readonly users: User[] = [
        {
            id: 1,
            name: "testname",
            username: "cooluser",
            password: "admin"
        }
    ];

    async existsUser(username: string): Promise<User | undefined> {
        return this.users.find( (user) => {
            user.username === username;
        });
    }
}

用户.module.ts

import { Module } from '@nestjs/common';
import { UsersService } from './users.service';

@Module({
    providers: [UsersService],
    exports: [UsersService]
})
export class UsersModule {}
typescript authentication nestjs passport-local nestjs-passport
1个回答
0
投票

哦!您的

AppMoule
需要导入您的
AuthModule
以便注册护照策略

© www.soinside.com 2019 - 2024. All rights reserved.