【问题标题】:Get unsigned token for user from firebase auth emulator without frontend从没有前端的firebase auth模拟器为用户获取未签名的令牌
【发布时间】:2021-12-30 12:36:28
【问题描述】:

我正在编写一个自定义后端 (nestjs),我想在其中验证来自 firebase auth 的令牌是否有效并检索用户信息。

我不想使用实际的 firebase 身份验证,所以我最终使用了 firebase 本地模拟器。

现在我想使用邮递员测试我用nestjs编写的端点,其中我从邮递员发送未签名的令牌,以便nestjs从本地模拟器进行验证。但是我找不到一种方法来创建一个未签名的令牌,而无需为其创建一个 UI,我真的不想花时间创建一个仅针对 console.log 令牌的反应应用程序。有没有更好的方法可以做到这一点,我可能会错过??

感谢您的帮助。

【问题讨论】:

    标签: firebase firebase-authentication nestjs firebase-admin


    【解决方案1】:

    假设您的身份验证模拟器在端口 9099 上运行并且您创建了一个用户,您应该能够发出以下 HTTP POST 请求来获取令牌。令牌位于响应对象的idToken 字段中。

    http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=any_key_you_want

    正文(JSON):

    {
      "email": "your-user@mail.com",
      "password": "some-password"   
    }
    

    回复:

    {
        "kind": "identitytoolkit#VerifyPasswordResponse",
        "registered": true,
        "localId": "yourUserId",
        "email": "your-user@mail.com",
        "idToken": "someIdToken",
        "refreshToken": "someRefreshToken",
        "expiresIn": "3600"
    }
    

    我发现这个解决方案使用了一个安装了firebase (^9.6.2) 包的 React 应用程序,设置了 connectAuthEmulator(auth, "http://localhost:9099");,并查看了它在我登录时发出的请求。

    【讨论】:

    • 非常感谢,它工作正常。
    猜你喜欢
    • 2016-12-27
    • 2021-04-04
    • 2021-02-26
    • 2020-10-29
    • 2020-03-30
    • 2021-08-03
    • 1970-01-01
    • 2018-01-24
    • 2021-01-04
    相关资源
    最近更新 更多