关于nestjs使用jwt按照官方文档出现中出现secretOrPrivateKey must have a value的解决方案

1. 主要环境

node v21.2.0

@nestjs/core  ^10.2.10

展现一下全部依赖

2. 问题定位

出现问题的地方,在创建access_token时候出错

[Nest] 40264  - 2023/12/04 09:55:37   ERROR [ExceptionsHandler] secretOrPrivateKey must have a value
Error: secretOrPrivateKey must have a value
    at Object.module.exports [as sign] (E:\nest-trpc\node_modules\jsonwebtoken\sign.js:111:20)
    at JwtService.sign (E:\nest-trpc\node_modules\@nestjs\jwt\dist\jwt.service.js:41:20)
    at AuthService.login (E:\nest-trpc\src\auth\auth.service.ts:30:44)
    at UsersController.login (E:\nest-trpc\src\users\users.controller.ts:20:29)
    at E:\nest-trpc\node_modules\@nestjs\core\router\router-execution-context.js:38:29
    at E:\nest-trpc\node_modules\@nestjs\core\router\router-execution-context.js:46:28
    at E:\nest-trpc\node_modules\@nestjs\core\router\router-proxy.js:9:17

3.解决方案

在检查N遍后,在stackoverflow找了半天没找到解决方法一步一步调试,发现不能按照官方文档这么写

 官方文档原文:

  async login(user: any) {
    const payload = { username: user.username, sub: user.userId };
    return {
      access_token: this.jwtService.sign(payload), //这一步出现问题
    };
  }

 这样改就行了

this.jwtService.sign(
    payload, 
    { secret: jwtConstants.secret } //添加你的secret
)
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
nestjs是一个基于Node.js的开发框架,它提供了一种简单而强大的方式来构建高效、可扩展的服务器端应用程序。而JWT(JSON Web Token)是一种用于身份验证和授权的开放标准,它可以在客户端和服务器之间安全地传输信息。 在nestjs使用JWT进行身份验证和授权非常简单。下面是使用nestjsjwt模块进行身份验证和授权的基本步骤: 1. 首先,安装`@nestjs/jwt`模块。可以使用npm或者yarn进行安装: ``` npm install --save @nestjs/jwt ``` 2. 在nestjs的模块导入`JwtModule`并配置密钥和其他选项。在`app.module.ts`文件添加以下代码: ```javascript import { Module } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; @Module({ imports: [ JwtModule.register({ secret: 'your-secret-key', // 密钥,用于签署和验证token signOptions: { expiresIn: '1h' }, // token的过期时间 }), ], }) export class AppModule {} ``` 3. 创建一个`AuthService`服务,用于生成和验证JWT。在`auth.service.ts`文件添加以下代码: ```javascript import { Injectable } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; @Injectable() export class AuthService { constructor(private readonly jwtService: JwtService) {} async generateToken(payload: any): Promise<string> { return this.jwtService.sign(payload); } async verifyToken(token: string): Promise<any> { return this.jwtService.verify(token); } } ``` 4. 在需要进行身份验证和授权的地方使用`AuthService`服务。例如,在一个控制器,可以使用`@UseGuards`装饰器和`AuthGuard`守卫来保护路由。在`auth.controller.ts`文件添加以下代码: ```javascript import { Controller, Post, UseGuards, Request } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { AuthService } from './auth.service'; @Controller('auth') export class AuthController { constructor(private readonly authService: AuthService) {} @Post('login') async login(@Request() req) { // 在这里进行用户身份验证,验证通过后生成token const token = await this.authService.generateToken({ username: req.body.username }); return { token }; } @Post('protected') @UseGuards(AuthGuard()) // 使用AuthGuard守卫保护路由 async protectedRoute() { // 在这里进行授权操作 return 'This route is protected'; } } ``` 这样,你就可以使用nestjsjwt模块进行身份验证和授权了。当用户登录成功后,会生成一个JWT token,并在后续请求使用该token进行授权。 希望以上信息对你有帮助!如果你还有其他问题,请继续提问。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值