【问题标题】:Delete all users from the new firebase auth emulator从新的 firebase auth 模拟器中删除所有用户
【发布时间】:2021-02-26 21:53:06
【问题描述】:

我正在使用新的 firebase auth 模拟器(在节点管理 SDK 上),如果我在每次测试之间手动删除创建的用户,我已经做了一些可以完美运行的测试,但我似乎无法自动删除他们?

我使用了在 beforeEach() 中定义的端点 here,但我从响应调用中得到了“响应代码 401,未经授权”?

端点:删除:http://localhost:9099/emulator/v1/projects/{project-id}/accounts

我刚刚尝试使用 Postman 发送呼叫,它的响应如下:

{
    "error": {
        "code": 401,
        "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
        "errors": [
            {
                "message": "Login Required.",
                "domain": "global",
                "reason": "required",
                "location": "Authorization",
                "locationType": "header"
            }
        ],
        "status": "UNAUTHENTICATED"
    }
}

错误中的 URL 似乎除了向网络应用程序添加一个 google 按钮之外没有给我太多帮助,这使我需要创建一个 OAuth2 网络帐户。我将 localhost:9099 输入到现有的,但不知道应该在哪里使用客户端 ID 和客户端密码?如果它们是我应该使用的。

我知道删除调用需要某种 Authorization 标头,但我只是不知道应该在该标头中放入什么或如何放入。

感谢您对此的任何见解。

编辑:我现在尝试了以下授权标头:

“管理员”

“”(一个空字符串)

firebase.options.credential.getAccessToken() 生成的完整令牌

上述token的access_token字段

上述token的id_token字段。

令牌本身看起来像这样(编辑了一些字段):

{ 
"access_token":
       "[access token string here]",
    "expires_in": 3599,
    "scope":
       "openid https://www.googleapis.com/auth/firebase https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/cloudplatformprojects.readonly",
    "token_type": "Bearer",
    "id_token":
       "[id token string here]"
}

【问题讨论】:

  • 尝试在您的请求中发送Authorization: Bearer owner 标头。但是,我怀疑您是否能够通过在 /accounts 端点上发出 DELETE 请求来删除所有用户。至少,prod Firebase Auth 服务不是这样工作的。
  • 据我从文档中可以看出,模拟器有这个端点。今天晚些时候我会试试这个,谢谢。
  • 端点确实存在,但DELETE /accounts不会删除所有帐户。
  • 这可能只在模拟器中支持。据我所知,生产 API 不支持它。这就是POST /accounts:batchDelete API 存在于 prod API 中的原因。
  • Emulator 接受 Bearer owner 作为 Authorization 标头。你试过吗?

标签: firebase firebase-authentication firebase-admin


【解决方案1】:

我想通了!当管理员生成令牌时,我使用此令牌的 access_token 字段部分并将标头 Authorization: 'Bearer' + access_token 添加到删除请求中。感谢您的帮助。

(这是一个仅限模拟器的端点)

编辑:我可以只使用字符串“所有者”作为令牌......我花了一段时间才得到它,但现在它可以工作了。

【讨论】:

    【解决方案2】:

    谢谢!但是,我想用 curl 格式在 npm 脚本上使用它,所以这是我使用的:

    curl -H 'Authorization: Bearer owner' -X DELETE http://localhost:9099/emulator/v1/projects/<projectid>/accounts
    

    响应应该是:

    {}
    

    【讨论】:

    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2020-10-29
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多