【发布时间】:2018-01-15 18:08:06
【问题描述】:
我想测试一个创建用户的云功能。
在正常情况下,我在浏览器中生成一个idToken,然后通过标头将其发送到服务器:Authorization : Bearer etcIdToken
但是我想在没有浏览器的情况下测试这个功能。在我的摩卡测试中,我有:
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 - getting the idToken like this works on client - and is documented here.
我直接尝试了signedInUser.getIdToken()。同样的问题:
signedInUser.getIdToken is not a function - 未记录。只是一个测试。
我认为这是因为 firebase 对象不适合 node.js 使用,就像我在这里所做的那样。登录时 - 东西被保存在浏览器本地存储中 - 也许这就是原因。
但问题仍然存在。如何在 node.js 中获取 idToken 以便能够进行测试:
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 它将起作用。我需要浏览器吗?谢谢:)
【问题讨论】:
-
您说 Firebase Web 客户端 SDK 不适合在节点应用程序中运行是正确的。为什么不使用已记录的 Cloud Functions 测试技术之一并跳过尝试模拟浏览器的功能? firebase.google.com/docs/functions/local-emulatorfirebase.google.com/docs/functions/unit-testing
-
您使用的是什么版本的
firebase客户端库?getIdToken是在 4.x.x 版本中引入的。 -
嘿,你解决了吗?我正在处理同样的事情。
标签: javascript node.js firebase firebase-authentication google-cloud-functions