首页 > 解决方案 > nestjs:Jwt 策略中的有效载荷未定义

问题描述

一切都很好,当我登录时,我得到了 jwt

但是当我想访问需要身份验证的资源时,在验证方法中,有效负载是未定义的

这是策略 jwt

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(
     private userService:UserService
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey:"aaaaaaa",
    });
  }

  async validate(payload:any) {
      console.log(payload)
      const user =await this.userService.findUserByEmail(payload.email)
      console.log(user)
      if(user){
          const {password,salt,...result}=user
          console.log(result)
          return result;
      }

    else throw new UnauthorizedException()
  }
}

这是模块

@Module({
  exports:[UserService],
  controllers: [UserController],
  providers: [UserService,JwtStrategy],
  imports : [TypeOrmModule.forFeature([UserEntity]),PassportModule.register(
    {defaultStrategy:'jwt'}),
  JwtModule.register({secret:'MedAKRAOU',signOptions:{expiresIn:3600000}})]
})
export class UserModule {}

标签: nestjs-jwt

解决方案


推荐阅读