next.js - 如何使用 next-auth 在 Next.js 中实现凭据授权?

标签 next.js next-auth

我尝试使用 next-auth 获取凭据身份验证,但我没有使用它的经验,无论我做什么,我都会收到以下消息:[next-auth][error][callback_credentials_jwt_error] Signin in with credentials is only supported if JSON Web Tokens are enabled https://next-auth.js.org/errors#callback_credentials_jwt_error .
也许,结果是一团糟,但我希望你能帮助我。
这是我的 src/pages/api/auth/[...nextauth].js 文件。

import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import User from "@models/User"

const options = {
    NEXTAUTH_URL:process.env.NEXTAUTH_URL,
    providers: [
        Providers.Credentials({
            // The name to display on the sign in form (e.g. 'Sign in with...')
            name: 'Avista',
            // The credentials is used to generate a suitable form on the sign in page.
            // You can specify whatever fields you are expecting to be submitted.
            // e.g. domain, username, password, 2FA token, etc.

            credentials: {
                email: {label: "Email", type: "text"},
                password: {label: "Password", type: "password"}
            },

            authorize: async (credentials) => {
                // Add logic here to look up the user from the credentials supplied
                // const user = {id: 1, name: 'J Smith', email: 'jsmith@example.com'}
                const user = await User.findOne()
                console.log("Данные входа", credentials)

                if (user) {
                    // Any object returned will be saved in `user` property of the JWT
                    return Promise.resolve(user)
                } else {
                    // If you return null or false then the credentials will be rejected
                    return Promise.resolve(null)
                    // You can also Reject this callback with an Error or with a URL:
                    // return Promise.reject(new Error('error message')) // Redirect to error page
                    // return Promise.reject('/path/to/redirect')        // Redirect to a URL
                }
            }
        }),
        Providers.Email({
            server: {
                host: process.env.EMAIL_SERVER_HOST,
                port: process.env.EMAIL_SERVER_PORT,
                auth: {
                    user: process.env.EMAIL_SERVER_USER,
                    pass: process.env.EMAIL_SERVER_PASSWORD
                }
            },
            from: process.env.EMAIL_FROM
        }),
    ],

    // A database is optional, but required to persist accounts in a database
    database: process.env.MONGO_URI,
    secret: process.env.JWT_SIGNING_PRIVATE_KEY,
    },
}
我不知道我做错了什么。我可以获得有关此主题的更多信息。

最佳答案

尝试将其添加到您的 [...nextauth].js 页面

  session: {
    jwt: true, 
    // Seconds - How long until an idle session expires and is no longer valid.
    maxAge: 30 * 24 * 60 * 60, // 30 days
  },
在此处阅读有关选项的更多信息:https://next-auth.js.org/configuration/options#jwt

关于next.js - 如何使用 next-auth 在 Next.js 中实现凭据授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64587059/

相关文章:

regex - 除了 "/"之外的所有页面的正则表达式是什么?

authentication - Next-Auth 凭据不返回 session 并且不通过 prisma 适配器将 session 和帐户存储在数据库中

javascript - 带有 NextJS 的 typescript

azure - Next Auth Active Directory 使用用户信息覆盖配置文件

security - 我需要在基于 cookie 的 API 中使用 CSRF token 吗?

javascript - 来自 Fauna DB 的逆向数据

reactjs - 当从 safari 使用 qr-code-styling 下载 QRcode 时,图像中心缺少 Logo

javascript - [下一个授权] : `useSession` must be wrapped in a <SessionProvider/> error on the existing js file

next.js - 无法在生产中使用 Next-auth 登录