typescript - Xác thực Nest.js sử dụng typeORM
Tôi đã tạo một máy chủ hoạt động với kết nối đến cơ sở dữ liệu, với một yêu cầu get đơn giản hoạt động. Tôi đã làm theo hướng dẫn tại https://docs.nestjs.com/techniques/authentication.
khi chạy máy chủ và cố gắng thực hiện một yêu cầu get từ một đường dẫn bảo vệ, tôi nhận được 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 {}
cũng khi chạy máy chủ tôi nhận được một cảnh báo [AuthGuard] Để sử dụng "defaultStrategy", vui lòng đảm bảo nhập PassportModule ở mỗi nơi mà AuthGuard() được sử dụng. Nếu không, passport sẽ không hoạt động đúng cách.