gpt4 book ai didi

authentication - next-auth GET/api/auth/session 400 CLIENT_FETCH_ERROR 意外 token 'E'(Netlify 部署)

转载 作者:行者123 更新时间:2023-12-05 04:21:51 32 4
gpt4 key购买 nike

问题:

我的 next-auth 在本地工作,但是当我将它部署到 Netlify 时,我在访问任何内容时都会收到此 CLIENT_FETCH_ERROR(RouteGuard 组件调用 useSession Hook 并推送到/auth/signin,如果未在非公共(public)路径上进行身份验证 &&)。

GET https://website.com/api/auth/session 400--> Error: This action with HTTP GET is not supported by NextAuth.js

[next-auth][error][CLIENT_FETCH_ERROR] Unexpected token 'E', "Error:Thi"... is not valid JSON

POST https://website.com/api/auth/_log 400

它在本地调用 http://localhost:4200/api/auth/session 并使用 {} 返回 304。

调用 useSession 钩子(Hook)的地方:

_app > session 提供者 session={session} > RouteGuard

根据文档预期的 API 返回:

https://next-auth.js.org/getting-started/client#getsession

When called, getSession() will send a request to /api/auth/session andreturns a promise with a session object, or null if no session exists.

https://next-auth.js.org/getting-started/rest-api#get-apiauthsession

Returns client-safe session object - or an empty object if there is no session.The contents of the session object that is returned are configurable with the session callback.

session 回调:

async session({ session, token, user }) {

session.accessToken = token.accessToken
session.user._id = token.sub
session.user.name = token.name
session.user.email = token.email
if (token.login_provider) session.user.login_provider = token.login_provider
return session

}

我已经在本地和 Netlify 上检查过的内容:

  • [...nextauth].ts 正确放置在/pages/api/auth 文件夹中
  • 它的命名与我上面写的完全一样
  • NEXTAUTH_URL 已正确设置(未使用任何自定义 basePath)
  • 使用 Next.js 运行时 - v4.27.3 也会自动设置它,尽管它的设置方式没有区别
  • NEXT_PUBLIC_API_URL 设置正确(相同的 url +/api)
  • NEXTAUTH_SECRET 已正确设置
  • NextAuthOptions 对象的页面:{ signIn: '/auth/signin' } 是正确的
  • /pages/auth/signin 页面已正确放置并命名
  • 凭证、Google 和 Facebook 提供商环境变量设置正确
"next": "11.1.0",
"next-auth": "^4.6.1",

我还应该检查什么?

Google 搜索、阅读文档、重新阅读文档并尝试了一天都无济于事......

请帮忙/o\

更新:

  • 做了更多研究,我不知道这是否重要,但是本地主机上的请求是使用 cookie 发送的,而这些 cookie 不会在部署版本上发送:

next-auth.csrf-token

next-auth.callback-url

更新 2:

  • getProviders 在 Netlify 的 getServerSideProps 中不起作用,我猜是因为它不喜欢 SSR

最佳答案

使用 next@12.3.1 和 next-auth@4.14.0 的确切版本修复了它。

在这里找到解决方案:https://answers.netlify.com/t/nexauth-returning-action-not-found/77779/5

关于authentication - next-auth GET/api/auth/session 400 CLIENT_FETCH_ERROR 意外 token 'E'(Netlify 部署),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74144025/

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