在Firebase中-如何在服务器上生成idToken以进行测试? [英] In firebase - How to generate an idToken on the server for testing purposes?

查看:227
本文介绍了在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 is not a function-获取idToken可以在客户端上使用-并且记录在这里.

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

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

signedInUser.getIdToken is not a function-未记录.只是测试.

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 :)

推荐答案

来自 Exchange自定义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天全站免登陆