FIREBASE WARNING: Provided authentication credentials are invalid - node.js

I am trying to use Firebase in node.js but every time I restart the server I am getting following error:
FIREBASE WARNING: Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your apiKey and databaseURL match the values provided for your app at https://console.firebase.google.com/, or if you're using a service account, make sure it's authorized to access the specified databaseURL and is from the correct project.
Following is my index.js:_
var express = require('express');
var router = express.Router();
var mongoose=require('mongoose');
var admin=mongoose.model('admin');
var firebase = require("firebase");
// Initialize the app with no authentication
firebase.initializeApp({
serviceAccount: {
projectId: "...",
clientEmail: "...",
privateKey: "-----BEGIN PRIVATE KEY-----...",
},
databaseURL: "..."
});
console.log("sfsaf")
// The app only has access to public data as defined in the Security Rules
var db = firebase.database();
var ref = db.ref("unitalk-b9145");
var messagesRef = ref.child("messages");
messagesRef.push({
name:"Rupali",
post:"Demo test of firebase"
});
Although I have checked the path of service-account and databaseURl..
Please help..

You can not log in with the service account using the "firabase" package. You need to use the "firabase-admin" package for this. You can find detailed information here (https://firebase.google.com/docs/database/admin/start).
UPDATED: 8 Nov 2016
go to : https://console.firebase.google.com
To use the Firebase Admin SDKs, you'll need a Firebase project, a service account to communicate with the Firebase service, and a configuration file with your service account's credentials.
Navigate to the Service Accounts tab in your project's settings
page.
Select your Firebase project. If you don't already have one, click
the Create New Project button. If you already have an existing
Google project associated with your app, click Import Google Project
instead.
Click the Generate New Private Key button at the bottom of the
Firebase Admin SDK section of the Service Accounts tab.
After you click the button, a JSON file containing your service
account's credentials will be downloaded. You'll need this to
initialize the SDK in the next step.
Sample code;
var admin = require("firebase-admin");
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

Another method to solve the issue link
for those who still facing this issue, you may try on this method, this is related to the project roles management according to the description inside

For anyone looking at this recently. I had the problem with Firebase function, after changing project. It went away when i removed the keys from admin.initializeApp()
Apparently firebase functions now know to use the project credentials. So just this;
admin.initializeApp();

You are using require('firebase') module so u need the following things:
var config = {
apiKey: " [your api key]",
authDomain: "[projectname].firebaseapp.com",
databaseURL: "https://[projectname].firebaseio.com/",
storageBucket: "[projectname].appspot.com",
messagingSenderId: "[message id]",
};
firebase.initializeApp(config);
If you want to use require("firebase-admin") then you have to configure
serviceAccountKey.json(downloaded file) file ...
I am able to connect with firebase successfully.

Related

Firebase Functions Service Accounts with Two Projects

I have two firebase projects, one for production and one for development. I've created service account keys for both projects and I initialize admin in my functions folder like so:
const admin = require("firebase-admin");
const serviceAccount = require("./service-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: "some-app.appspot.com",
});
const db = admin.firestore();
module.exports = {
db,
admin,
};
But I need firebase to use the appropriate service account for each project. My app is setup to use the production project when deployed in production, but the way I've set up admin.js (above) it always takes the development service-account credentials.
How can I set the service account credentials as an environment variable? Is there a way to add the key to firebase config in the console? Or should I just hard code in the production service account and live with that?
I found this in the docs, but it doesn't appear to solve my problem.
serviceAccount = require('./serviceAccount.json');
const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG);
adminConfig.credential = admin.credential.cert(serviceAccount);
admin.initializeApp(adminConfig);
I appreciate any help you can provide.
You could specify the configuration to use in an environment variable as shown here and then pick that up in your code.
But that should not be necessary in most cases, since calling admin.initializeApp() without parameters already initializes the Admin SDK with the administrative credentials for the current project.

firebase cannot determine project id

this is the request that I made using node
// Initialize the default app
var admin = require('firebase-admin');
var app = admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: process.env.FIREBASE_DATABASE
});
console.log(process.env.FIREBASE_DATABASE);
router.post('/', (req, res, next) => {
app.auth().getUserByEmail("j.100233260#gmail.com")
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
console.log('Successfully fetched user data:', userRecord.toJSON());
res.json(userRecord.toJSON())
})
.catch(function(error) {
console.log('Error fetching user data:', error);
res.json(error)
});
}
);
I set the env var on my machine
for my firebase database I used env
given as
databaseURL: "https://fssssss.firebaseio.com",
from the firebase admin GUI ,
the error in Postman when I request this route
{
"code": "app/invalid-credential",
"message": "Failed to determine project ID: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND"
}
I followed the docs and google returned no results, no idea what to do. thanks.
This isn't working the way you expect:
admin.credential.applicationDefault()
You can't use this on a server that you control without additional configuration. This only works by itself when running on Google services when you want to use the default service account for your project. When running on your own server, there is no default. You have to be explicit and download service account credentials to use during initialization. At the very least, you'll need to follow the instructions in the documentation:
To authenticate a service account and authorize it to access Firebase
services, you must generate a private key file in JSON format.
To generate a private key file for your service account:
In the Firebase console, open Settings > Service Accounts.
Click Generate New Private Key, then confirm by clicking Generate Key.
Securely store the JSON file containing the key.
When authorizing via a service account, you have two choices for
providing the credentials to your application. You can either set the
GOOGLE_APPLICATION_CREDENTIALS environment variable, or you can
explicitly pass the path to the service account key in code. The first
option is more secure and is strongly recommended.
So you will need to download the service account file, set GOOGLE_APPLICATION_CREDENTIALS correctly, then your code will work. The service account credentials are not optional.
Method 1: Just pass the path of your service account key
const firebase_admin = require('firebase-admin');
const serviceAccount = require("/path/to/yourserviceaccountkey.json");
const admin = firebase_admin.initializeApp({
credential: firebase_admin.credential.cert(serviceAccount);
});
Method 2 (more secure): The method above is considered less secure than the method below:
const firebase_admin = require('firebase-admin');
const admin = firebase_admin.initializeApp({
credential: admin.credential.applicationDefault()
});
Then run the app like this:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/yourserviceaccountkey.json node index.js
Or through a package.json script:
"start" : "GOOGLE_APPLICATION_CREDENTIALS=/path/to/yourserviceaccountkey.json node index.js"
You can also set the environmental variable likes this in a terminal session, before you run Node:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
References
Add the Firebase Admin SDK to your server
If you followed the instructions for initializing the SDK by setting the environment variable using this command:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
You may have to double check that the name of the service account file matches the path you downloaded.
For example when I downloaded the service-account-file.json, mine was named my-firebase-project.json, but I used the export command without updating the filename.
Once that was fixed, the issue disappeared.
You need to add projectId
admin.initializeApp({
credential: admin.credential.applicationDefault(),
projectId: `xxx-xxxxxx-xxx`, // this line
databaseURL: environment.firebaseDbUrl
});
Note: You'll get the projectId from firebase project settings page. See image
Reason why you are getting this error even after setting the GOOGLE_APPLICATION_CREDENTIALS environment variable correctly is because it needs to be set in every session when you run the firebaseAdmin initialize method. Hence you should set it every time using below in your package.json file:
"scripts": {
"build": "npx tsc",
"start": "export GOOGLE_APPLICATION_CREDENTIALS=/Users/abc/Documents/Code/my_folder/my_app_6abcd31ffa3a.json npm run build && ts-node index.ts",
"test": "test"
},

How to use firebase oauth as a login tool for vps applications.?

Hello all firebase experts,
I just learned about node.js today, and I intend to use firebase oauth as a login tool in my application and install the application on vps, is there a suggestion that the NPM package should be used? or is there a simple example that I can understand?
At server side (Node) you won't need to OAuth but you may need to OAuth as Admin. And for this;
var admin = require("firebase-admin");
// Fetch the service account key JSON file contents
var serviceAccount = require("path/to/serviceAccountKey.json");
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databaseName.firebaseio.com"
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("restricted_access/secret_document");
ref.once("value", function(snapshot) {
console.log(snapshot.val());
});

Firebase: How to verify Google login token id from Android & iOS

I want to verify on my node backend all the tokens (Google login) i get from my Android app. I started with initializing the firebase module like this:
var admin = require('firebase-admin');
var serviceAccount = require('googlefirebase');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: url
});
This gave me some error about some DEFAULT name and i found out that i needed to use this code:
var admin = require('firebase-admin');
var serviceAccount = require('googlefirebase');
admin.initializeApp(functions.config().firebase);
Then i realize that i need to install and init the project on my server so i did this:
firebase login
firebase init
firebase use --add project
firebase functions:config:set "google-services.json" (just the project_info data of the json that i downloaded from firebase)
Now i get this error:
Error: Must initialize app with a cert credential or set your Firebase
project ID as the GOOGLE_CLOUD_PROJECT environment variable to call
verifyIdToken()
EDIT START
I get this error when i call:
admin.auth().verifyIdToken(token).then(function(decodedToken) {}.catch(){};
EDIT END
I already "init" firebase (or at least i thing so) and created the environment variables: GOOGLE_CLOUD_PROJECT and FIREBASE_CONFIG and i keep getting the same error.
So, whats the right way to get firebase to work? what am i missing? is verifyIdToken the right method to verify the token? i just want to verify the google login token.
With Cloud Functions for Firebase, you're not supposed to initialize like this any more:
admin.initializeApp(functions.config().firebase);
You're supposed to use no arguments:
admin.initializeApp();
Also, your google-services.json file is not useful in Cloud Functions. That's only for use in an Android app.
you should have a FIREBASE_CONFIG environments variable and then call
// Initialize the default app
var admin = require('firebase-admin');
var app = admin.initializeApp();
that config variable contain the initialize information like :
databaseURL: 'https://databaseName.firebaseio.com',
storageBucket: 'projectId.appspot.com',
projectId: 'projectId'

configuring firebase realtime database

Already i have initialized firebase with firebase functions and deployed. I am using fulfillment and i am not using separate server. i am using firebase only, ie. I have developed javascript code and deployed in firebase itself.
Now I want to configure firebase realtime database with this project.
Can I use below sample code?
var admin = require("firebase-admin");
// Fetch the service account key JSON file contents
var serviceAccount = require("path/to/serviceAccountKey.json");
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databaseName.firebaseio.com"
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("restricted_access/secret_document");
ref.once("value", function(snapshot) {
console.log(snapshot.val());
});
I have few questions:
where to start configuring firebase realtime database?
How to get file "serviceAccountKey.json"? since,i am not using separate server.
How to get database URL?
Did OAuth configuration is required before database?
I believe since V1 you don't need to configure the admin since it takes the config form the server by itself checkout this link for more info https://firebase.google.com/docs/functions/beta-v1-diff#new_initialization_syntax_for_firebase_admin

Resources