Next.js认证模块遇到JWE解密失败问题。

26

我正在使用此代码以便在Cognito作为OAuth服务时与next-auth提供程序一起使用凭据:这是为了允许电子邮件和密码身份验证。我正在运行next-auth@4.2.1:

import CognitoProvider from "next-auth/providers/cognito";
import NextAuth from 'next-auth'
import CredentialsProvider from "next-auth/providers/credentials"
import * as cognito from '../../../lib/cognito'
import { Auth } from 'aws-amplify';

export default NextAuth({
    providers: [
        CredentialsProvider({
            credentials: {
              username: { label: "Username", type: "text", placeholder: "jsmith" },
              password: {  label: "Password", type: "password" }
            },
            async authorize(credentials, req) {
                try {
                    const user = await Auth.signIn(credentials.username, credentials.password);
                    return user
                } catch (error) {
                    console.log('error signing in', error);
                }
            }
          })
    ],
    debug: process.env.NODE_ENV === 'development' ? true : falsey

})

我经常遇到这个错误:

https://next-auth.js.org/errors#jwt_session_error decryption operation failed {
  message: 'decryption operation failed',
  stack: 'JWEDecryptionFailed: decryption operation failed\n' +
    '    at gcmDecrypt (/home/aurel/Documents/repos/front/node_modules/jose/dist/node/cjs/runtime/decrypt.js:67:15)\n' +
    '    at decrypt (/home/aurel/Documents/repos/front/node_modules/jose/dist/node/cjs/runtime/decrypt.js:92:20)\n' +
    '    at flattenedDecrypt (/home/aurel/Documents/repos/front/node_modules/jose/dist/node/cjs/jwe/flattened/decrypt.js:119:52)\n' +
    '    at async compactDecrypt (/home/aurel/Documents/repos/front/node_modules/jose/dist/node/cjs/jwe/compact/decrypt.js:18:23)\n' +
    '    at async jwtDecrypt (/home/aurel/Documents/repos/front/node_modules/jose/dist/node/cjs/jwt/decrypt.js:8:23)\n' +
    '    at async Object.decode (/home/aurel/Documents/repos/front/node_modules/next-auth/jwt/index.js:64:7)\n' +
    '    at async Object.session (/home/aurel/Documents/repos/front/node_modules/next-auth/core/routes/session.js:41:28)\n' +
    '    at async NextAuthHandler (/home/aurel/Documents/repos/front/node_modules/next-auth/core/index.js:96:27)\n' +
    '    at async NextAuthNextHandler (/home/aurel/Documents/repos/front/node_modules/next-auth/next/index.js:21:19)\n' +
    '    at async /home/aurel/Documents/repos/front/node_modules/next-auth/next/index.js:57:32',
  name: 'JWEDecryptionFailed'
}

在文档中找到了https://next-auth.js.org/errors#jwt_session_error,但并没有真正帮助到我。

谢谢。

5个回答

47

刚才只需添加一个秘密就能让它工作

export default NextAuth({
    secret: process.env.AUTH_SECRET,
    providers: [
    ...
    ]
})

2
这个似乎在版本^4.23.1上不起作用。有什么想法?:D - undefined

30
下一个认证(NextAuth)需要 NEXTAUTH_SECRET 环境变量来加密JWT并哈希电子邮件验证令牌。你可以将其放置在 .env 文件中,例如:
NEXTAUTH_SECRET=say_lalisa_love_me_lalisa_love_me_hey

请参考NextAuth参考文档


1
这是我缺失的一部分,请确保添加它,并相应地更改密码。 - undefined

8

NEXTAUTH_SECRET 用于加密 NextAuth.js JWT 和哈希电子邮件验证令牌。这是 NextAuth 和 Middleware 中 secret 选项的默认值。

获取更多详细信息,请访问:https://next-auth.js.org/configuration/options#secret

JWTKeySupport:该密钥不支持 HS512 验证算法。

获取更多详细信息,请访问:https://next-auth.js.org/errors#jwt_session_error

请按照以下步骤解决该问题。

步骤 1:使用以下命令生成您的随机密钥

openssl rand -base64 32

第二步:您可以像这样在.env文件中添加NEXTAUTH_SECRET。
NEXTAUTH_SECRET=YOUR_KEY_HERE,

或者,像这样在next.config.js文件中添加

const config = {
  reactStrictMode: true,
  env: {
    NEXTAUTH_SECRET:"YOUR_KEY_HERE",
  },
};

export default config;

步骤3:在[...nextauth].ts中添加一个秘密。
 export const nextOption = {
  
  secret: process.env.NEXTAUTH_SECRET as string,
...<rest of your code>

0

1
欢迎来到StackOverflow!您应该将评论或示例复制到响应中。链接本身可能会更改,然后此答案就变得不完整了。此外,在链接中指向的内容也不明显。这作为答案单独存在并不太合适,更适合作为对答案的评论。 - crollywood

0
 import { getToken } from "next-auth/jwt"

const secret = process.env.NEXTAUTH_SECRET

export default async function handler(req, res) {
  // if using `NEXTAUTH_SECRET` env variable, we detect it, and you won't actually need to `secret`
  // const session= await getToken({ req })
  const session= await getToken({ req, secret })
  console.log("JSON Web Token", session)
  res.end()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接