首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NextAuth.js: JWT秘密破解应用程序

NextAuth.js: JWT秘密破解应用程序
EN

Stack Overflow用户
提问于 2021-11-30 18:57:00
回答 3查看 6.4K关注 0票数 5

我使用Next.js (11.1.2) + NextAuth (4.0.0-beta.7)登录到Strapi中,只使用凭据提供程序(JWT)。

整个流程都与此[...nextauth].js“工作”。

代码语言:javascript
运行
复制
import NextAuth from "next-auth"
import CredentialsProvider from 'next-auth/providers/credentials'

export default NextAuth({
  providers: [
    CredentialsProvider({
      name: 'AppName',
      credentials: {
        email: {label: "Email", type: "text", placeholder: "daveglow@foomail.com"},
        password: {  label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        const res = await fetch(process.env.CREDENTIALS_AUTH_URL, {
          method: 'POST',
          body: JSON.stringify(credentials),
          headers: { "Content-Type": "application/json" }
        })
        const user = await res.json()

        if (res.ok && user) {
          return user
        }
        return null
      }
    })
  ],
  session: {
    strategy: "jwt",
    maxAge: 30 * 24 * 60 * 60 // 30 days
  },
  pages: {
    signIn: '/signin',
    signOut: '/signin',
    error: '/signin'
  },
})

但在用户登录几秒钟后,终端显示此消息并删除会话:

代码语言:javascript
运行
复制
[next-auth][warn][NO_SECRET] https://next-auth.js.org/warnings#no_secret
[next-auth][error][JWT_SESSION_ERROR] https://next-auth.js.org/errors#jwt_session_error decryption operation failed {
  message: 'decryption operation failed',
  stack: 'JWEDecryptionFailed: decryption operation failed\n'

所以,我试着补充:

代码语言:javascript
运行
复制
secret: process.env.SECRET, //I've created using $ openssl rand -base64 32

然后我收到两条不同的信息

浏览器控制台

代码语言:javascript
运行
复制
[next-auth][error][CLIENT_FETCH_ERROR] 
https://next-auth.js.org/errors#client_fetch_error

与代码终端

代码语言:javascript
运行
复制
[next-auth][error][CALLBACK_CREDENTIALS_JWT_ERROR] 
https://next-auth.js.org/errors#callback_credentials_jwt_error Signin in with credentials only supported if JWT strategy is enabled UnsupportedStrategy [UnsupportedStrategyError]: Signin in with credentials only supported if JWT strategy is enabled

我试过几种不同的选择,但这让我很困惑。现在,我不知道该怎么做。) :(你能帮我吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-12-02 09:45:24

我升级到4.0.1版。解决了这个问题。

票数 2
EN

Stack Overflow用户

发布于 2021-12-01 13:20:15

正如https://github.com/nextauthjs/next-auth/issues/3216中所讨论的,它可能是在beta 7版本下引入的一个bug。

票数 1
EN

Stack Overflow用户

发布于 2022-01-07 20:04:44

我不是专家。但我认为这是当前图书馆的一个问题,下一个4.0.0

我可以使用版本“next”:"^3.25.0“来解决这个问题。然后跟着这个教程

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

https://stackoverflow.com/questions/70174647

复制
相关文章

相似问题

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