Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js -Firebase Service Account Private Key won't parse

I use .env variables in my app.js file to access the keys. Everything was working fine until I downloaded a new Firebase Service Account Private Key. When I replaced the old value with the new value I can no longer access the key because in terminal when I run node app.js I keep getting an error message:

/Users/Cpu/Desktop/...../node_modules/firebase-admin/lib/auth/credential.js:129 throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse private key: ' + error); ^

Error: Failed to parse private key: Error: Invalid PEM formatted message. at FirebaseAppError.FirebaseError [as constructor] (/Users/Cpu/Desktop/...../node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseAppError.PrefixedFirebaseError [as constructor] (/Users/Cpu/Desktop/...../node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseAppError (/Users/Cpu/Desktop/...../node_modules/firebase-admin/lib/utils/error.js:119:28) at new Certificate (/Users/Cpu/Desktop/...../node_modules/firebase-admin/lib/auth/credential.js:129:19) at new CertCredential (/Users/Cpu/Desktop/...../node_modules/firebase-admin/lib/auth/credential.js:192:64) at Object.cert (/Users/Cpu/Desktop/.....) at Object. (/Users/Cpu/Desktop/...../app.js:14:32) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at FirebaseAppError.FirebaseError [as constructor] npm ERR! code ELIFECYCLE npm ERR! errno 1

All I did was c+p the new Private Key and then added it and saved the .env file, pushed to heroku, and it's no longer working. I even downloaded a new Private Key but the same problem occurs.

The old and new Private Keys

// old Private Key -----BEGIN PRIVATE KEY-----\nbbbbbbbb\n-----END PRIVATE KEY-----\n  // new Private Key -----BEGIN PRIVATE KEY-----\nzzzzzzzz\n-----END PRIVATE KEY-----\n 

The .env file:

FIREBASE_PROJECT_ID=wwwwwwww FIREBASE_CLIENT_EMAIL=xxxxxxxx FIREBASE_DATABASE_URL=yyyyyyyy FIREBASE_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\nzzzzzzzz\n-----END PRIVATE KEY-----\n 

The app.js file:

const dotenv = require('dotenv'); dotenv.load();  var admin = require("firebase-admin"); admin.initializeApp({   credential: admin.credential.cert({       projectId: process.env.FIREBASE_PROJECT_ID,   // I get no error here       clientEmail: process.env.FIREBASE_CLIENT_EMAIL,   // I get no error here       privateKey: process.env.FIREBASE_PRIVATE_KEY   // I get error HERE   }),   databaseURL: process.env.FIREBASE_DATABASE_URL }); 

How can I fix this issue?

like image 994
Lance Samaria Avatar asked May 11 '18 19:05

Lance Samaria


People also ask

How do I secure a Node JS web application with express?

This tutorial demonstrates how to secure a Node.js web application built with the Express framework by implementing user authentication. You'll enhance a starter Node.js project to practice the following security concepts: Add user login and logout. Retrieve user information. Protect application routes. Call protected endpoints from an API.

How to enable user authentication with Auth0 in Node JS?

The Complete Guide to Node.js User Authentication with Auth0 1 Get the Starter Application 2 Connect Express with Auth0 3 Set Up Express OpenID Connect 4 Add User Authentication 5 Retrieving User Information 6 Protecting Routes 7 Calling an API 8 Conclusion More ...

How do I install NodeJS and NPM?

If you need to install Node.js and npm, use any of the official Node.js installers provided for your operating system. This tutorial's core objective is to teach you how to set up real-world authentication in a Node.js Express app.

Is there a Parse Server backend for Android?

[EXPERIMENTAL] React, React Native, and React with SSR (e.g. Next.js) packages to interact with Parse Server backend Parse LiveQuery client for Android. ParseUI contains user interface libraries for building apps with the Parse Android SDK.


2 Answers

The problem was since I used dotenv variables inside the .env file the FIREBASE_PRIVATE_KEY had escaping characters: \n inside of it.

I had to follow this answer and append .replace(/\\n/g, '\n') to the end of it to parse it:

privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n') 

So now the code looks like:

admin.initializeApp({   credential: admin.credential.cert({       projectId: process.env.FIREBASE_PROJECT_ID, // I get no error here       clientEmail: process.env.FIREBASE_CLIENT_EMAIL, // I get no error here       privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n') // NOW THIS WORKS!!!   }),   databaseURL: process.env.FIREBASE_DATABASE_URL }); 
like image 144
Lance Samaria Avatar answered Oct 01 '22 09:10

Lance Samaria


You must add your key into double qoutes to allow expanded new lines option according with dotenv documentation.

You can check that option at Rules section in dotenv github.

https://github.com/motdotla/dotenv#rules

  FIREBASE_PROJECT_ID=wwwwwwww   FIREBASE_CLIENT_EMAIL=xxxxxxxx   FIREBASE_DATABASE_URL=yyyyyyyy   FIREBASE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nzzzzzzzz\n-----END PRIVATE KEY-----\n" 
like image 31
Bryan Ramirez Avatar answered Oct 01 '22 08:10

Bryan Ramirez