当使用 Auth0 作为第三方访问提供者时,Fauna 返回 'unauthorized'

Fauna returning 'unauthorized' when using Auth0 as a third party access provider

我正在尝试将 Fauna 和 Auth0 集成到我的 Vue 3 应用程序中。

为了实现这一目标,我正在关注 this Auth0 guide and this youtube video

简而言之,我已经将 Auth0 配置为 Fauna 内部的 Provider。我正在发送 Auth0 生成的 JWT 令牌作为 Fauna 秘密。然后 Fauna 应该解码 JWT 并授予访问权限。

为了测试它,我的代码从 Fauna 获取了一些虚拟的“产品”数据并将其打印到控制台。

但是当我调用它时 returns 作为 unauthorized.

我做错了什么?

这是我的 Vue 组件中进行调用的脚本:

import { defineComponent, inject } from "vue";
import { query as q, Client } from "faunadb";

export default defineComponent({
  name: "Api",
  setup() {
    let apiMessage = null;
    let executed = false;
    const auth = inject("Auth");

    const callApi = async () => {
      const accessToken = await auth.getTokenSilently();
      console.log(accessToken);
      try {
        const client = new Client({ secret: accessToken });
        const { Paginate, Documents, Collection } = q;

        const data = await client.query(
          Paginate(Documents(Collection("products")))
        );

        console.log(data);
        apiMessage = data;
        executed = true;
      } catch (e) {
        console.log(e);
        apiMessage = `Error: the server responded with '${e.response.status}: ${e.response.statusText}'`;
      }
    };

    return {
      callApi,
    };

  },
});

这是返回的 unauthorized 响应对象的副本:

{
  "name": "Unauthorized",
  "message": "unauthorized",
  "description": "Unauthorized",
  "requestResult": {
    "method": "POST",
    "path": "",
    "query": null,
    "requestRaw": "{\"paginate\":{\"documents\":{\"collection\":\"products\"}}}",
    "requestContent": {
      "raw": {
        "paginate": {
          "raw": {
            "documents": {
              "raw": {
                "collection": "products"
              }
            }
          }
        }
      }
    },
    "responseRaw": "{\"errors\":[{\"code\":\"unauthorized\",\"description\":\"Unauthorized\"}]}",
    "responseContent": {
      "errors": [
        {
          "code": "unauthorized",
          "description": "Unauthorized"
        }
      ]
    },
    "statusCode": 401,
    "responseHeaders": {
      "content-length": "65",
      "content-type": "application/json;charset=utf-8",
      "x-txn-time": "1634006015704445"
    },
    "startTime": 1634006014934,
    "endTime": 1634006015885
  }
}

想通了。

客户端必须是 initiated with some other values,最重要的是 domain 值。

var client = new faunadb.Client({
  secret: 'YOUR_FAUNA_SECRET',
  domain: 'db.fauna.com',
  // NOTE: Use the correct domain for your database's Region Group.
  port: 443,
  scheme: 'https',
})