Firebase Authentication for Realm Cloud using JWT and a Google Cloud Function

328 views Asked by At

I am attempting to use JWTs to authenticate into Realm Cloud. I am using Firebase as the authentication service and am attempting to create a Google Cloud Function to generate the JWT. I generated the private and public keys using the terminal command "ssh-keygen". Realm's JWT tutorial suggested the following line of code to read the key file:

const key = fs.readFileSync('./functions/id_rsa', 'utf8'); 

I copied the private key over to the project, pointed the code above to the file, but when I deploy the Google Cloud Function, I received the following error message:

âš  functions[myAuthFunction(us-central1)]: Deployment error. Function failed on loading user code. Error message: Code in file index.js can't be loaded. Is there a syntax error in your code? Detailed stack trace: Error: ENOENT: no such file or directory, open './functions/id_rsa'

My project structure is as follows: Picture

I have attempted to ask on Realm's forums but I haven't received much help. The entire cloud function they suggested is:

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 }
});

In summary, how can the google cloud function read my private key file in my project? The public key is stored inside my Realm Cloud dashboard for the specific instance.

Sources: Realm Cloud JWT Firebase tutorial

1

There are 1 answers

0
ashhanai On

Try passing just ./id_rsa or id_rsa. I believe the path root is where the index.js file is.