首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在尝试用fastify-jwt签署jwt时未定义Fastify

在尝试用fastify-jwt签署jwt时未定义Fastify
EN

Stack Overflow用户
提问于 2021-10-08 18:01:17
回答 1查看 538关注 0票数 0

在我的处理程序中,我希望能够签署一个JWT,但是没有定义'fastify‘。

代码语言:javascript
复制
const postJoinHandler = async (
  request: any,
  reply: any
): Promise<{ id: string; name: string }> => {
  try {
    const { username, password } = request.body;
    const token = fastify.jwt.sign({ username, password }); //<=== Fastify not defined
    return reply.code(201).send(token);
  } catch (error) {
    request.log.error(error);
    return reply.send(400);
  }
};

我的图式..。

代码语言:javascript
复制
import { postJoinHandler } from '../handlers/auth';

const Token = {
  type: 'object',
  properties: {
    username: { type: 'string' },
    password: { type: 'string' },
  },
};


const postJoinSchema = {
  schema: {
    body: {
      type: 'object',
      required: ['username', 'password', 'email', 'fullname'],
      properties: {
        name: { type: 'string' },
        password: { type: 'string' },
      },
    },
    response: {
      201: Token,
    },
  },
  handler: postJoinHandler,
};

export { postJoinSchema };

还有我的路线

代码语言:javascript
复制
import { FastifyPluginAsync } from 'fastify';
import { postJoinSchema } from '../schemas/auth';

const auth: FastifyPluginAsync = async (fastify): Promise<void> => {
  fastify.post('/auth/join', postJoinSchema);
};

export default auth;

我已经把fastify-jwt作为插件加载了

代码语言:javascript
复制
const fp = require('fastify-plugin');

module.exports = fp(async function (fastify: any) {
  fastify.register(require('fastify-jwt'), {
    secret: 'Supersecret!@#',
  });

  fastify.decorate('authenticate', async function (request: any, reply: any) {
    try {
      await request.jwtVerify();
    } catch (err) {
      reply.send(err);
    }
  });
});

docs 这里

EN

回答 1

Stack Overflow用户

发布于 2022-10-11 11:07:44

您可以通过请求访问Fintify实例。只要做request.server。服务器是可以访问插件等的实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69500012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档