【问题标题】:Next-Auth with FaunaDB Dev Get Session Error使用 FaunaDB Dev 获取会话错误的 Next-Auth
【发布时间】:2021-10-26 21:28:03
【问题描述】:

我已经在我的 NextJS 项目上设置了 Next-Auth 以使用 FaunaDB。我已经设法让它在我的生产环境中运行,但是,它似乎不适用于我的开发环境,它使用的是 Fauna Dev docker 容器而不是基于云的数据库。

每当我运行开发服务器时,我都会收到以下错误日志;

https://next-auth.js.org/errors#fauna__get_session_error [NotFound: instance not found] {
  description: 'Set not found.',
  requestResult: RequestResult {
    method: 'POST',
    path: '',
    query: null,
    requestRaw: '{"get":{"match":{"index":"session_by_token"},"terms":"86b962d600c3f53d41d5140de6daed654a473e670a99943d7fb301a4fc86357e"}}',
    requestContent: Expr { raw: [Object] },
    responseRaw: '{"errors":[{"position":[],"code":"instance not found","description":"Set not found."}]}',
    responseContent: { errors: [Array] },
    statusCode: 404,
    responseHeaders: [Object: null prototype] {
      ':status': 404,
      'x-txn-time': '1630008523944000',
      'x-compute-ops': '1',
      'x-read-ops': '0',
      'x-byte-read-ops': '0',
      'x-byte-write-ops': '0',
      'x-write-ops': '0',
      'x-query-time': '703',
      'x-query-bytes-in': '121',
      'x-query-bytes-out': '87',
      'x-storage-bytes-read': '0',
      'x-storage-bytes-write': '0',
      'x-txn-retries': '0',
      'x-faunadb-build': '21.08.00-4d315fa',
      'content-length': '87',
      'content-type': 'application/json;charset=utf-8'
    },
    startTime: 1630008521471,
    endTime: 1630008522280
  }
}

我已按照指南进行设置并继续遇到相同的错误。我已经多次删除并重新创建了动物 docker 容器,但仍然是同样的错误。我还有一些可以在本地 Fauna Dev 数据库中访问的数据,所以似乎设置得很好。此外,如果我将.env.local 环境变量更改为我在生产中使用的环境变量,一切正常,但当然是使用云数据库。

这是我的生产变量和局部变量的比较,删除了私钥;

# .env.local
GOOGLE_ID=<removed>
GOOGLE_SECRET=<removed>
NEXT_PUBLIC_FAUNA_ADMIN_KEY=<removed>
NEXT_PUBLIC_FAUNA_DOMAIN=localhost
NEXT_PUBLIC_FAUNA_GRAPHQL_ENDPOINT=http://localhost:8084/graphql
NEXT_PUBLIC_FAUNA_PORT=8443
NEXT_PUBLIC_FAUNA_SCHEME=http
NEXTAUTH_URL=http://localhost:3000

# production env
GOOGLE_ID=<removed, identical to local>
GOOGLE_SECRET=<removed, identical to local>
NEXT_PUBLIC_FAUNA_ADMIN_KEY=<removed, different to local>
NEXT_PUBLIC_FAUNA_DOMAIN=db.eu.fauna.com
NEXT_PUBLIC_FAUNA_GRAPHQL_ENDPOINT=https://graphql.eu.fauna.com/graphql
NEXT_PUBLIC_FAUNA_SCHEME=https
NEXTAUTH_URL=https://dnd-compendium-five.vercel.app

另外,这是我的[...nextauth].ts 文件;

import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import * as Fauna from "faunadb";
import { FaunaAdapter } from "@next-auth/fauna-adapter";

const isProduction = process.env.NODE_ENV === "production";


const client = new Fauna.Client({
  secret: process.env.NEXT_PUBLIC_FAUNA_ADMIN_KEY,
  scheme: process.env.NEXT_PUBLIC_FAUNA_SCHEME === "https" ? "https" : "http",
  domain: process.env.NEXT_PUBLIC_FAUNA_DOMAIN,
  ...(isProduction ? {} : { port: +process.env.NEXT_PUBLIC_FAUNA_PORT }),
});

export default NextAuth({
  // Configure one or more authentication providers
  providers: [
    Providers.Google({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
    }),
    // ...add more providers here
  ],
  adapter: FaunaAdapter({ faunaClient: client }),
});

更新

这是来自生产动物数据库的几份文件,个人信息已删除;

# accounts collection document
{
  "ref": Ref(Collection("accounts"), "307944413618766018"),
  "ts": 1629937528150000,
  "data": {
    "userId": "307944413481402562",
    "providerId": "google",
    "providerType": "oauth",
    "providerAccountId": "110736403335097897627",
    "accessToken": "ya29.a0ARrdaM_n4D6gDvzs8Mxs851r70Xs5v5HROSdcDM0HJoMP1XFhAPhFbmh46e4bfHzTbVYOhoc2W7N_0IHpV3yEW5a__Nzy8NrxWU3ns-KlxIm_oTXRVW0RyitT26uXtTq-45k6OTjakjHsinYpnof4qdXcICg",
    "createdAt": Time("2021-08-26T00:25:28.101705Z"),
    "updatedAt": Time("2021-08-26T00:25:28.101705Z")
  }
}

# sessions collection document
{
  "ref": Ref(Collection("sessions"), "308011284890124486"),
  "ts": 1630001301580000,
  "data": {
    "userId": "307946306265940166",
    "expires": Time("2021-09-25T18:08:21.521Z"),
    "sessionToken": "997b45bb432cfbc05901e9a92ae9d1ac36637246277ac05ad915fb00b8fc9130",
    "accessToken": "9dbec0113a46bc6a0768e1a5b7baafac65a1fb0a29568bc11562cd58ad97d5b9",
    "createdAt": Time("2021-08-26T18:08:21.517447Z"),
    "updatedAt": Time("2021-08-26T18:08:21.517447Z")
  }
}

# users collection document
{
  "ref": Ref(Collection("users"), "307946306265940166"),
  "ts": 1629939333135000,
  "data": {
    "name": <string removed>,
    "email": <string removed>,
    "image": <url string removed>,
    "createdAt": Time("2021-08-26T00:55:33.075553Z"),
    "updatedAt": Time("2021-08-26T00:55:33.075553Z")
  }
}

【问题讨论】:

  • 这似乎是一个查询/数据问题,而不是环境问题。您能否向我们展示导致instance not found 错误的查询,以及所涉及的任何索引的定义?加上一份隐藏 PII/敏感数据的示例文档?
  • @eskwayrd 我不确定正在运行什么查询,因为它是由 next-auth 在后台自动运行的。已设置的索引与指南页面上的索引相同 - next-auth.js.org/adapters/fauna#schema 我已使用生产站点中的一些文档更新了该问题。希望这就是你所需要的,但如果我误解了,请告诉我。感谢您对此进行调查!
  • 您可以尝试的一件事是删除session_by_token 索引,然后重新创建它。如果这有所作为,那对我们的工程团队来说将是有用的信息。
  • @eskwayrd 没用,很遗憾。我删除了索引并得到错误描述"Ref refers to undefined index 'session_by_token'"。然后我将其重新添加并得到与我开始时相同的 'Set not found.' 错误描述。
  • 您是否已将 fauna-shell 配置为连接到您的 Fauna Dev 映像?如果是这样,你能告诉我Paginate(Match(Index("session_by_token"), "&lt;a valid token id&gt;")) 的输出吗?

标签: next.js faunadb next-auth


【解决方案1】:

您使用的是哪个版本的 next-auth 和适配器?他们最近发布了 v4 测试版和匹配的适配器测试版。也许你会对这些新版本有更多的运气?

【讨论】:

    猜你喜欢
    • 2022-12-25
    • 2022-01-21
    • 2021-08-17
    • 1970-01-01
    • 2022-12-11
    • 2021-11-08
    • 2022-12-04
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多