我尝试使用 JWT 在 NestJS API 中对用户进行身份验证,但总是收到相同的错误:未经授权 401。
在我告诉你我的调查结果是空的之前,让我向你展示我的代码:
auth.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { UserModule } from 'src/user/user.module';
import { AuthService } from './auth.service';
import { LocalAuthController } from './local/local-auth.controller';
import { JwtStrategy } from './jwt/jwt.strategy';
@Module({
imports: [
ConfigModule,
JwtModule.register({
secretOrPrivateKey: 'a'
}),
UserModule],
providers: [AuthService, JwtStrategy],
controllers: [LocalAuthController]
})
export class AuthModule {}
Run Code Online (Sandbox Code Playgroud)
auth.service.ts
import { Injectable, UnauthorizedException } from …Run Code Online (Sandbox Code Playgroud)