【发布时间】:2023-02-06 12:12:15
【问题描述】:
我正在尝试在 Next.js (v12.3.2) 的中间件中验证 JWT 令牌,但在使用 jsonwebtoken 时遇到错误(遵循此处的文档:https://clerk.dev/docs/request-authentication/validate-session-tokens),因为它需要 Node 环境。
我尝试使用 jose 代替 CLERK_JWT_KEY 代替 Clerk.dev 但我一直收到 [错误:关键信息没有必需的参数] 错误。作为参考,这是我的代码的样子:
export const decodeAndVerifyToken = async (
getToken: ServerGetToken
): Promise<JWTPayload | undefined> => {
// initialize a variable for the token
let token: string | null;
try {
// get the token using metadata template, which should return
// a 'publicMetadata' object containing an 'isAdmin' value
token = await getToken({ template: "metadata" });
} catch (err) {
// if we had an error getting the token, return undefined
return undefined;
}
// if no token is found, then short-circuit to undefined
if (!token) {
return undefined;
}
// split the jwt key to 64-bit lines
const splitPem = process.env.CLERK_JWT_KEY?.match(/.{1,64}/g) ?? [];
// combine into a public key format
const publicKey =
"-----BEGIN PUBLIC KEY-----\n" +
splitPem.join("\n") +
"\n-----END PUBLIC KEY-----";
//
const test = await importSPKI(publicKey, "ES256").catch((err) =>
console.log(err)
);
};
我也试过直接调用 const decoded = await jwtVerify(token, publicKey); 但这也会产生错误。
有谁知道如何解决?
【问题讨论】:
标签: reactjs next.js jwt vercel clerk