Firebase: How to programmatically generate a jwt t

2019-05-27 13:15发布

问题:

I have a http Cloud Function that expects a jwt token to be passed in the Authorization header (similar to Will's answer in this question: How to protect firebase Cloud Function HTTP endpoint to allow only Firebase authenticated users?). This cloud function verifies the token with the function admin.auth().verifyIdToken(<token>). In my client code, I use the code firebase.auth().currentUser.getIdToken(false) to generate a token to include in the http request in the aforementioned header.

I want to be able to generate this token given a user's uid programmatically, preferably in Node, so that I can write tests for my function without having to go through my web app to get the token.

I tried using admin.auth().createCustomToken(<uid>) but that doesn't create a token that can be verified with admin.auth().verifyIdToken. I also tried following the steps in this guide https://firebase.google.com/docs/database/rest/auth, however the token generated is not one that verifyIDToken accepts, though I am able to make REST calls to the database using it (as mentioned in the guide).