Help with Cloud Function and pathToMyPrivateKeyFile

cloud

#1

Following the docs at https://docs.realm.io/platform/v/3.x/guides/firebase-authentication-with-realm-cloud.

const functions = require(“firebase-functions”);
const jwt = require(‘jsonwebtoken’);
const fs = require(‘fs’);
const key = fs.readFileSync(’pathToMyPrivateKeyFile’);
exports.myAuthFunction = functions.https.onCall((data, context) => {
const uid = context.auth.uid,
const payload = { userId: uid },
const token = jwt.sign(payload, { key: key, passphrase: “your-passphrase” }, { algorithm: ‘RS256’}),
return { token:token }
});

I’m looking for documentation around ‘pathToMyPrivateKeyFile’. I’ve generated the public/private keys, enabled JWT for my project on the Realm Platform Dashboard, but could use some guidance with the private key path and getting the private key on my project instance server…


#2

Did you ever figure this out? I have the same question.


#3

You deployed the pem file, along with the function, to Firebase - right?


#4

I’ve copied the pem format of the public key to the Realm console and enabled JWT. So far, so good.

The Firebase Cloud Function code has this line:

const key = fs.readFileSync(’pathToMyPrivateKeyFile’);

The private key file is on my computer. I don’t have my own server. I assume I have to copy the file to Firebase (using Firebase Storage?) and then get a path to the file. The instructions are lacking about this.


#5

OK, I figured this out. For the next poor soul who doesn’t regularly write node js stuff, do this:

  1. Copy your private key file to the functions directory in your cloud functions project
  2. Replace
    const key = fs.readFileSync(’pathToMyPrivateKeyFile’);
    with
    const key = fs.readFileSync(path.resolve(__dirname, “myPrivateKeyFilename”));