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

标签 authentication deployment netlify next-auth dev-to-production

问题:

我的 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 and returns 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/

相关文章:

reactjs - 在同一个存储库中的 github 页面前端和后端上部署 React 应用程序

windows - 在最终用户系统上调试 "application configuration is incorrect"问题的好方法?

javascript - 使用 Netlify URL 问题部署的 React Redux 应用程序

javascript - gatsbyjs 中的 bulma 菜单切换不起作用

php - PHP 中的安全身份验证

security - JWT 和 OAuth2 - 服务器是否存储 token ?以及它们如何安全/黑客安全?

python - 什么是 post_form_id? (使用Python urllib2)

azure - 如何解决 Azure AD 上的错误消息 "There was an issue looking up your account. Tap Next to try again."

c# - 在 Debug模式下发布了一个 asp.net Web 应用程序?

javascript - Gridsome 项目托管在 netlify 上。点击时,即使 URL 更新,页面也不会加载