我创建了一个工作服务器,该服务器与数据库连接,并带有一个简单的get
请求,该请求有效。
我遵循了https://docs.nestjs.com/techniques/authentication本指南。
当运行服务器并尝试从受保护的路由执行get
请求时,我得到Cannot read property 'challenge' of undefined
。
user.controller.ts
import { Controller, Get, UseGuards } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.entity';
import { AuthGuard, PassportModule } from '@nestjs/passport';
@Controller()
export class UserController {
constructor(
private readonly userService: UserService)
{}
@Get('user')
@UseGuards(AuthGuard())
findAll(): Promise<User[]> {
return this.userService.findAll();
}
@Get('users')
find(): Promise<User[]> {
return this.userService.findAll();
}
}
auth.module.ts
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { HttpStrategy } from './http.strategy';
import { UserModule } from 'database/entities/user/user.module';
import { PassportModule } from '@nestjs/passport';
const passportModule = PassportModule.register({ defaultStrategy: 'bearer' });
@Module({
imports: [
passportModule,
UserModule],
providers: [AuthService, HttpStrategy],
exports: [passportModule],
})
export class AuthModule {}
在运行服务器时,我也收到警告[AuthGuard] In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.