I want to test a a cloud function that creates users.
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));
})
The error i'm getting is:
firebase.auth(...).currentUser.getIdToken is not a function
- getting the idToken like this works on client - and is documented here.
I tried directly with signedInUser.getIdToken()
. Same problem:
signedInUser.getIdToken is not a function
- not documented. just a test.
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.
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: "[email protected]",
password: "123456"
})
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 :)
firebase
client library are you using?getIdToken
was introduced in version 4.x.x. – Versicolor