16
votes

Using the new Firebase Cloud Functions in combination with the admin sdk.

I want to use the admin.auth().createCustomToken() function. Calling this function results in a error message

Error: createCustomToken() requires a certificate with "private_key" set.
    at FirebaseAuthError.Error (native)
    at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)
    at FirebaseTokenGenerator.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/token-generator.js:62:19)
    at Auth.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/auth.js:89:37)
    at /user_code/index.js:29:26
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)

How do I config the cloud functions to use a private_key?

admin.initializeApp(functions.config().firebase);
2
@davidtabmann: please don't tag with firebase-functions. Proper tagging for Cloud Functions for Firebase is [google-cloud-functions][firebase]. Also see my answer here: stackoverflow.com/questions/42854865/… - Frank van Puffelen
@FrankvanPuffelen , ok, I've indeed read that as stated here, I was first waiting for an answer from you in there...Now it's very clear for me. - DavidTaubmann

2 Answers

40
votes

Unfortunately, the createCustomToken() method requires a private key to mint custom tokens, which is not currently available with the default credential (which happens to be an Application Default Credential). As noted in Create custom tokens using the Firebase Admin SDKs, you need to provide a certificate credential to be able to create custom tokens.

You can generate the certificate needed for this credential by following the instructions in Add Firebase to your app. Once you have the key JSON file, you need to get it into Cloud Functions for Firebase.

You can do this by storing the key JSON file in your /functions folder as service-account.json. Then, in the file where you define your Functions, use admin.credential.cert() to initialize the Admin SDK, like this:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

var serviceAccount = require("./service-account.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: functions.config().firebase.databaseURL
});

For a full example of how to do this, with more detailed instructions and a code sample, check out the Instagram sign in sample.

Note that we hope to add support for createCustomToken() from the default credential in the future, but for now, you will have to bring your own credential for this particular method to work.

0
votes

As stated here: https://firebase.google.com/docs/auth/admin/create-custom-tokens?authuser=0

To test the same code locally, download a service account JSON file and set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to it.

Then in your code:

admin.initializeApp();