3
votes

There is not much information on creating your own customToken on the Firebase Docs. It would be helpful for beginner developers to know how to create them to sort data to be accessible for specific users in an app (ios-swift), e.g. group1 has specific users who can view a specific section of the database.

In my case, I get lost in step 3 from 'Authenticate with Firebase'

  1. When users sign into your app, send their sign-in credentials (for example, their username and password) to your authentication server. Your server checks the credentials and returns a custom token if they are valid.

from: https://firebase.google.com/docs/auth/ios/custom-auth

I am unsure about sending the sign-in credentials back to my server, where I will create a custom token and send it back. But how am I supposed to do this? Firebase Docs doesn't specify how.

I have set a server with node.js with the following code:

var firebase = require('firebase');
var admin = require('firebase-admin');
var FirebaseTokenGenerator = require("firebase-token-generator");
var path = require('path');
var servAcc = '/Users/myUserName/Desktop/nodeClient/service-account.json';

var tokenGenerator = new FirebaseTokenGenerator("firebase-secret");
var token = tokenGenerator.createToken({
uid: "clientId", groupId: "group1", managerId:"MG1"
});

    firebase.initializeApp({

        serviceAccount: path.resolve(__dirname, '/Users/myUserName/Desktop/nodeClient/service-account.json'),
        databaseURL: "https://<app-name>.firebaseio.com/",
        databaseAuthVariableOverride: {
        uid: "clientId"
      }

    })

    admin.initializeApp({
      credential: admin.credential.cert(servAcc),
      databaseURL: "https://apos-accentit.firebaseio.com/"
    });

Just in admin.credential.cert(servAcc), I get an error in the terminal using nodemon.

Cannot read property of 'cert' undefined

How shall I proceed?

2

2 Answers

2
votes

I solved this problem by updating firebase-admin. Apparently, the new admin.credential.cert() method was added on Nov 7th, 2016 with firebase-admin version 4.0.0. I was using a tutorial that referenced an older version of firebase-admin (3.0.0), and that version was what my package.json file referenced. Once I referenced the 4.0.0. I no longer experienced the error.

https://firebase.google.com/support/release-notes/admin/node

1
votes

If you already have the json downloaded for your service credentials, create a 'js' file named credentials.js in the same folder of your main project. In this file create a variable called credentials and make it equal the json you have downloaded as your 'service-account.json'; also don't forget to export it at the end of your file as:

module.exports = credentials;

Save this file and at the top your main index.js file, import the newly created file named credentials.js as:

var serviceAccount = require('./credentials.js')

Now use this variable as your credentials. Inside of admin.initializedApp should look like this:

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<NAME OF YOUR DATABASE>.firebaseio.com"

});

in short. The reason why node can't read your service-account.json is because the file extension is not readable by node. I had the same problem and this fixed it for me