Firebase Cloud Functions - createCustomToken
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.