在 firebase - 如何在服务器上生成 idToken 用于测试目的? [英] In firebase - How to generate an idToken on the server for testing purposes?

查看:36
本文介绍了在 firebase - 如何在服务器上生成 idToken 用于测试目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个创建用户的云功能.

I want to test a a cloud function that creates users.

在正常情况下,在浏览器中我生成一个 idToken 并通过标头将其发送到服务器:Authorization : Bearer etcIdToken

In normal cases, inside the browser i generate an idToken and i send it to server via headers: Authorization : Bearer etcIdToken

但是我想在没有浏览器的情况下测试这个功能.在我的摩卡测试中,我有:

But I want to test this function without the browser. In my mocha tests i have:

before(done => {
   firebase = require firebase.. -- this is suppose to be like the browser lib.
   admin = require admin.. 

    idToken = null;
    uid = "AY8HrgYIeuQswolbLl53pjdJw8b2";
    admin.auth()
        .createCustomToken(uid)               -- admin creates a customToken
        .then(customToken => {
            return firebase.auth()            -- this is like browser code. customToken get's passed to the browser.
                .signInWithCustomToken(customToken)     -- browser signs in.
                .then(signedInUser => firebase.auth()             -- now i want to get an idToken. But this gives me an error.
                    .currentUser.getIdToken())
        })
        .then(idToken_ => {
            idToken = idToken_
            done();
        })
        .catch(err => done(err));
})

我得到的错误是:

firebase.auth(...).currentUser.getIdToken 不是函数 - 像这样在客户端上获取 idToken - 并在此处记录.

我直接尝试使用 signedInUser.getIdToken().同样的问题:

I tried directly with signedInUser.getIdToken(). Same problem:

signedInUser.getIdToken 不是函数 - 未记录.只是一个测试.

signedInUser.getIdToken is not a function - not documented. just a test.

我认为这是因为 firebase 对象不适合 node.js 使用,就像我在这里所做的那样.登录时 - 东西会保存在浏览器本地存储中 - 也许这就是原因.

I think this is because firebase object is not intended for node.js use like i'm doing here. When signing in - stuff get's saved in browser local storage - and maybe this is why.

但问题仍然存在.如何在 node.js 中获取 idToken 以便能够进行测试:

But the question still remains. How can i get an idToken inside node.js in order to be able to test:

return chai.request(myFunctions.manageUsers)
    .post("/create")
    .set("Authorization", "Bearer " + idToken)   --- i need the idToken here -  like would be if i'm getting it from the browser.
    .send({
          displayName: "jony",
          email: "jony@gmail.com",
          password: "123456"
    })

我是不是搞错了?我知道如果我能得到 idToken 它将起作用.我需要浏览器吗?谢谢:)

am I approaching this wrong? I know that if i can get the idToken it will work. Do i rely need the browser for this? Thanks :)

推荐答案

来自 交换自定义用于 ID 的令牌和刷新令牌,您可以使用 api 将自定义令牌转换为 id 令牌.因此,您只需首先从 uid 生成自定义令牌,然后将其转换为自定义令牌.这是我的示例:

From Exchange custom token for an ID and refresh token, you can transform a custom token to an id token with the api. Hence, you just have to generate a custom token first from the uid, then transform it in a custom token. Here is my sample:

const admin = require('firebase-admin');
const config = require('config');
const rp = require('request-promise');

module.exports.getIdToken = async uid => {
  const customToken = await admin.auth().createCustomToken(uid)
  const res = await rp({
    url: `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${config.get('firebase.apiKey')}`,
    method: 'POST',
    body: {
      token: customToken,
      returnSecureToken: true
    },
    json: true,
  });
  return res.idToken;
};

这篇关于在 firebase - 如何在服务器上生成 idToken 用于测试目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆