使用.env文件中的值并在createSecretKey中使用它会返回错误



我基本上是这样做的:

const secretKey = crypto.createSecretKey(
Buffer.from(process.env.SECRET, "hex")
);

我得到了以下错误:" value of "key. bylength "在射程之外。一定是>0. 收到0">

将process.env.SECRET替换为secret本身有效。

export const createToken = async (id) => {
const secretKey = crypto.createSecretKey(
Buffer.from(process.env.SECRET, "hex")
);
return await new EncryptJWT({ id })
.setProtectedHeader({ alg: "dir", enc: "A256GCM" })
.setIssuedAt()
.setExpirationTime("14d")
.encrypt(secretKey);
};  

有同样的问题,您的process.env.SECRET可能未定义。确保它存在:)

最新更新