gpt4 book ai didi

reactjs - TypeError : options. 提供程序在下一步身份验证中不可迭代

转载 作者:行者123 更新时间:2023-12-03 08:02:06 26 4
gpt4 key购买 nike

所以我一直在尝试将 Next Auth 集成到全栈 NextJS Web 应用程序中。我已经使用 JWT 处理了登录,并且由于使用了带有 Next Auth 的凭据登录类型,因此我将 session 存储在浏览器上。我已经设法让登录正常工作,但是我现在正在尝试保护我的 API 路由以从 MongoDB 检索用户的基本数据。相关文件如下,但是,我遇到了 unstable_getServerSession() 的错误(至少根据跟踪),该错误表明 options.providers 不可迭代。我无法理解这一点,因为我已将其创建为数组......

任何帮助将不胜感激!

[...nextauth].js

import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import clientPromise from '../../../lib/mongodb'
import { compare } from 'bcryptjs'

export default NextAuth({
session: {
strategy: "jwt"
},
providers: [
CredentialsProvider({
name: "Email and Password",
credentials: {
email: { label: "Email", type: "email", placeholder: "Email Address" },
password: { label: "Password", type: "password", placeholder: "Password" }
},
async authorize(credentials, req) {
const db = (await clientPromise).db();
const employees = db.collection('employees');
const res = await employees.findOne({
email: credentials.email
});
if (!res) {
throw new Error('No user found with those details');
}
const checkPassword = await compare(credentials.password, res.password);
if (!checkPassword) {
throw new Error('Incorrect username or password');
}
return res;
}
}),
],
pages: {
signIn: "/signin"
}
})

getEmployeeData.js

import { unstable_getServerSession } from "next-auth/next"
import authOptions from "../auth/[...nextauth]"
import clientPromise from "../../../lib/mongodb";

export default async function getEmployeeData(req, res) {
const session = await unstable_getServerSession(req, res, authOptions);
if (session) {
res.status(200).json({ success: "You got it!" });
}
}

错误消息

error - TypeError: options.providers is not iterable
at assertConfig (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/core/lib/assert.js:68:34)
at NextAuthHandler (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/core/index.js:70:52)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async unstable_getServerSession (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/next/index.js:79:19)
at async getEmployeeData (webpack-internal:///(api)/./pages/api/employees/getEmployeeData.js:13:21)
at async Object.apiResolver (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/api-utils/node.js:366:9)
at async DevServer.runApi (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/next-server.js:481:9)
at async Object.fn (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/next-server.js:735:37)
at async Router.execute (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/router.js:247:36)
at async DevServer.run (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/base-server.js:347:29) {
page: '/api/employees/getEmployeeData'
}
error - TypeError: options.providers is not iterable
at assertConfig (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/core/lib/assert.js:68:34)
at NextAuthHandler (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/core/index.js:70:52)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async unstable_getServerSession (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/next/index.js:79:19)
at async getEmployeeData (webpack-internal:///(api)/./pages/api/employees/getEmployeeData.js:13:21)
at async Object.apiResolver (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/api-utils/node.js:366:9)
at async DevServer.runApi (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/next-server.js:481:9)
at async Object.fn (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/next-server.js:735:37)
at async Router.execute (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/router.js:247:36)
at async DevServer.run (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/base-server.js:347:29) {
page: '/api/employees/getEmployeeData'
}
error - TypeError: options.providers is not iterable
at assertConfig (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/core/lib/assert.js:68:34)
at NextAuthHandler (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/core/index.js:70:52)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async unstable_getServerSession (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next-auth/next/index.js:79:19)
at async getEmployeeData (webpack-internal:///(api)/./pages/api/employees/getEmployeeData.js:13:21)
at async Object.apiResolver (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/api-utils/node.js:366:9)
at async DevServer.runApi (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/next-server.js:481:9)
at async Object.fn (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/next-server.js:735:37)
at async Router.execute (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/router.js:247:36)
at async DevServer.run (/Users/MYNAME/Programming/Work/Dan/HR-System/fullstack/node_modules/next/dist/server/base-server.js:347:29) {
page: '/api/employees/getEmployeeData'

我知道,同样的错误重复了三次,也不知道为什么会发生......

最佳答案

您必须单独导出 authOptions 并将它们作为参数应用于导出默认 NextAuth(authOptions)

这是您的代码的解决方案:

export const authOptions = {
session: {
strategy: "jwt"
},
providers: [
CredentialsProvider({
name: "Email and Password",
credentials: {
email: { label: "Email", type: "email", placeholder: "Email Address" },
password: { label: "Password", type: "password", placeholder: "Password" }
},
async authorize(credentials, req) {
const db = (await clientPromise).db();
const employees = db.collection('employees');
const res = await employees.findOne({
email: credentials.email
});
if (!res) {
throw new Error('No user found with those details');
}
const checkPassword = await compare(credentials.password, res.password);
if (!checkPassword) {
throw new Error('Incorrect username or password');
}
return res;
}
}),
],
pages: {
signIn: "/signin"
}
}

export default NextAuth(authOptions)

关于reactjs - TypeError : options. 提供程序在下一步身份验证中不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73826348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com