我正在做一个简单的登录函数,它将返回一个带有用户ID的令牌。
我已经用键在fastify
实例上注册了fastify
,并在对符号函数的调用中添加了用户id。
代码auth.js
import Fastify from "fastify";
import fastifyJwt from "fastify-jwt";
import { users } from "../dummy-data/users.js";
const fastify = Fastify();
const SECRET_KEY = "secret";
fastify.register(fastifyJwt, { secret: SECRET_KEY });
......
const signIn = function (req, res) {
const { email, password } = req.body;
const foundUser = users.find(
(user) => user.email === email && user.password === password
);
const { id } = foundUser;
const accessToken = fastify.jwt.sign({ id }, SECRET_KEY);
res.status(200).send({ accessToken });
}
但这不起作用,它显示了以下错误:
"message":“预期”选项成为普通对象“”。
{
"statusCode": 500,
"error": "Internal Server Error",
"message": "Expected \"options\" to be a plain object."
}
有人知道正确的方法吗?
发布于 2021-08-30 10:15:05
签名时不需要传递SECRET_KEY
const accessToken = fastify.jwt.sign({ id });
因为它已经在注册时传递给jwt-plugin:
fastify.register(fastifyJwt, { secret: SECRET_KEY });
https://stackoverflow.com/questions/68981443
复制相似问题