typescript - Nest.js 使用 typeORM 进行身份验证
问题描述
我创建了一个连接到数据库的工作服务器,一个简单的get
请求可以工作。我遵循了https://docs.nestjs.com/techniques/authentication本指南。
当运行服务器并尝试get
从受保护的路由执行请求时,我得到Cannot read property 'challenge' of undefined
.
用户控制器.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.
解决方案
推荐阅读
- c# - 按字典 LINQ 分组
- bigdata - 是否有任何用于实现分区/副本存储系统的开源框架?
- python - Python:如果列表中的第一个元素重复并且列表系列中的第二个元素最低,则删除列表中的列表
- unity3d - 游戏对象的颜色变化
- c++ - 试图从文本文件中读取向量
- json - 用jq结合相关数据
- spotfire - 我可以为用户指定一个“根”文件夹吗?
- json - Parsing nested JSON with Vuejs
- android - FragmentScenario configuration - Binary XML file line, Error inflating class
while testing with espresso - javascript - NodeJS use EJS can't get new html (query from MySQL)