无法使用 PassportJS 读取未定义的 属性 'validateUser',NestJS
Cannot read property 'validateUser' of undefined ,NestJS using PassportJS
我只想在 NestJS 中通过 passportjs 实现身份验证。
当我向“localhost:3000/auth/login”发送 post 请求时,我收到了这些错误行。
[ExceptionsHandler] Cannot read property 'validateUser' of undefined
TypeError: Cannot read property 'validateUser' of undefined
这是我的代码:
local.strategy.ts
import { AuthService } from './auth.service';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { UnauthorizedException } from '@nestjs/common';
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
async validate(username: string, password: string): Promise<any> {
console.log(username, password);
const user = await this.authService.validateUser(username, password); <====This part
if (!user) {
return new UnauthorizedException();
}
return user;
}
}
auth.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AuthService {
users = [{ id: 1, username: 'Peyman', password: 'password' }];
async validateUser(username: string, password: string): Promise<any> {
console.log('Validate');
const user = await this.users.find((x) => x.username === username);
if (user && user.password === password) {
return user;
}
return null;
}
}
auth.module.ts
import { LocalStrategy } from './local.strategy';
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { AuthService } from './auth.service';
@Module({
imports: [PassportModule],
providers: [AuthService, LocalStrategy],
exports: [PassportModule],
})
export class AuthModule {}
app.module.ts
import { Contact } from './contacts/contacts.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Module } from '@nestjs/common';
import { ContactsModule } from './contacts/contacts.module';
import { AppController } from './app.controller';
import { AuthModule } from './auth/auth.module';
import { PassportModule } from '@nestjs/passport';
@Module({
imports: [
ContactsModule,
PassportModule,
TypeOrmModule.forRoot({ entities: [Contact] }),
AuthModule,
],
controllers: [AppController],
providers: [],
})
export class AppModule {}
app.controller.ts
import { Controller, Post, UseGuards, Request } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Controller()
export class AppController {
@UseGuards(AuthGuard('local'))
@Post('auth/login')
public async login(@Request() req): Promise<any> {
return req.user;
}
}
我通过 依赖注入 注入 authservice,但出现错误。
我该如何解决这个问题?
将 @Injectable()
添加到 LocalStrategy
class。
手动删除 dist 文件夹并重新启动服务器对我有用
我只想在 NestJS 中通过 passportjs 实现身份验证。 当我向“localhost:3000/auth/login”发送 post 请求时,我收到了这些错误行。
[ExceptionsHandler] Cannot read property 'validateUser' of undefined
TypeError: Cannot read property 'validateUser' of undefined
这是我的代码:
local.strategy.ts
import { AuthService } from './auth.service';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { UnauthorizedException } from '@nestjs/common';
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
async validate(username: string, password: string): Promise<any> {
console.log(username, password);
const user = await this.authService.validateUser(username, password); <====This part
if (!user) {
return new UnauthorizedException();
}
return user;
}
}
auth.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AuthService {
users = [{ id: 1, username: 'Peyman', password: 'password' }];
async validateUser(username: string, password: string): Promise<any> {
console.log('Validate');
const user = await this.users.find((x) => x.username === username);
if (user && user.password === password) {
return user;
}
return null;
}
}
auth.module.ts
import { LocalStrategy } from './local.strategy';
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { AuthService } from './auth.service';
@Module({
imports: [PassportModule],
providers: [AuthService, LocalStrategy],
exports: [PassportModule],
})
export class AuthModule {}
app.module.ts
import { Contact } from './contacts/contacts.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Module } from '@nestjs/common';
import { ContactsModule } from './contacts/contacts.module';
import { AppController } from './app.controller';
import { AuthModule } from './auth/auth.module';
import { PassportModule } from '@nestjs/passport';
@Module({
imports: [
ContactsModule,
PassportModule,
TypeOrmModule.forRoot({ entities: [Contact] }),
AuthModule,
],
controllers: [AppController],
providers: [],
})
export class AppModule {}
app.controller.ts
import { Controller, Post, UseGuards, Request } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Controller()
export class AppController {
@UseGuards(AuthGuard('local'))
@Post('auth/login')
public async login(@Request() req): Promise<any> {
return req.user;
}
}
我通过 依赖注入 注入 authservice,但出现错误。 我该如何解决这个问题?
将 @Injectable()
添加到 LocalStrategy
class。
手动删除 dist 文件夹并重新启动服务器对我有用