在 firebase 中 - 如何在服务器上生成 idToken 以进行测试? [英] In firebase - How to generate an idToken on the server for testing purposes?
问题描述
我想测试一个创建用户的云功能.
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屋!