【问题标题】:Fauna returning 'unauthorized' when using Auth0 as a third party access provider使用 Auth0 作为第三方访问提供者时,Fauna 返回“未经授权”
【发布时间】:2021-10-13 08:08:07
【问题描述】:

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

为了实现这一点,我关注this Auth0 guide 和这个youtube video

简而言之,我已将 Auth0 配置为 Fauna 内部的提供程序。我将 Auth0 生成的 JWT 令牌作为 Fauna 密码发送。然后 Fauna 应解码 JWT 并授予对调用的访问权限。

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

但是当我拨打电话时,它会返回 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
  }
}

【问题讨论】:

    标签: vuejs3 auth0 faunadb


    【解决方案1】:

    想通了。

    客户端必须是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',
    })
    

    【讨论】:

      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 2021-09-20
      • 2017-02-12
      • 2021-04-16
      • 2020-03-29
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      相关资源
      最近更新 更多