typescript - Next-Auth 凭证提供者 - [next-auth][错误][客户端获取错误]
我想尝试使用 Next.js 和 next-auth 结合凭证提供程序进行身份验证。但每当我登录并尝试打开受保护的路由时,总会出现以下错误:
[next-auth][error][client_fetch_error]
https://next-auth.js.org/errors#client_fetch_error session FetchError: invalid json response body at http://localhost:3000/api/auth/session reason: Unexpected token < in JSON at position 0
at C:\Users\me\workspace\vscode\projects\next-auth-typescript-example\node_modules\node-fetch\lib\index.js:272:32
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
type: 'invalid-json'
}
要重现该错误,您可以执行以下步骤:
- 查看官方的 Next-Auth Typescript 示例:Next-Auth Typescript 示例
- 按照 next-auth 存储库的步骤操作
- 然后打开文件
/pages/api/auth/[...nextauth].ts - 使用我的代码添加凭证提供程序
- 启动服务器并尝试通过凭证提供程序使用任何凭证登录
- 尝试打开受保护的路线
- 看看你的控制台,你会发现错误
凭证提供程序的代码:
Providers.Credentials({
name: "Ogame Credentials",
credentials: {
username: { label: "Username", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
const user: User = {
id: 1,
name: "J Smith",
email: "[email ",
image: "https://google.de",
}
if (user) {
return user
} else {
return null
}
},
}),
这段代码也来自 next-auth 仓库的示例。
答案#1
好的,我找到了问题。我为我的 Python 环境安装了 miniconda。我没有注意到 miniconda 默认会挂载到 PowerShell(在 PowerShell 行前显示 base)
所以我在 PowerShell 中禁用了 miniconda,现在它可以正常工作了。