The documentation for creating cloud function testing directs the user to configure the test suite.
Here's the code block it directs the user to create:
// At the top of test/index.test.js
const test = require('firebase-functions-test')({
databaseURL: 'https://my-project.firebaseio.com',
storageBucket: 'my-project.appspot.com',
projectId: 'my-project',
}, 'path/to/serviceAccountKey.json');
We wish to use online mode, but we wish to test against the local firebase emulator instead of a cloud instance. We attempted to initialize the test by omitting the key file and redirecting the database url as below:
require('firebase-functions-test')({
databaseURL: 'localhost:9000',
storageBucket: 'my-project.appspot.com',
projectId: 'my-project',
});
Unfortunately this does not work. The program will just throw the exception:
#firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND"."}
How is a user supposed to hookup to an emulator rather than a cloud instance?
Related
This part
const serviceAccount = require('./firestore-reactdb-027d52fa212f.json');
Results in an error:
Module not found: Error: Can't resolve './firestore-reactdb-027d52fa212f.json' in (...)
I downloaded the json file from IAM & admin > Service accounts in the Cloud Platform Console (as instructed here under "initialize on your own server"). I tried also to download the credentials from the console in firebase (project settings -> service account -> generate new private key).
PS: it's in a react app
Those credentials are needed when you make your own service. React app is not a service read about what service is. If you want to use firebase in react app you don't need those secrets. You need firebase config object and you no need to wory about to sharing it. This config object is necesery for firebase to know where he can send requests etc.
Example object with configurations for firebase.
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "..."
};
I'm trying to use the firebase admin sdk in a local node.js script file (outside of firebase-functions) in order to preform some auth and firestore operations.
But I get this error:
{"severity":"WARNING","message":"Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail"}
Error: Failed to determine project ID: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND
Admin SDK initialization:
const admin = await import('firebase-admin');
const serviceKey = require('path/to/serviceKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceKey),
databaseURL: 'https://{project}.firebaseio.com',
});
All the questions I've found on this error are regarding running firebase-functions locally, but I'm not trying to use firebase-functions, I'm only trying to use admin.auth and admin.firestore locally.
Am I missing something here?
It works with the codes below based on this documentation:
const admin = require('firebase-admin');
const serviceKey = require('./path/to/serviceKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceKey),
databaseURL: 'https://{project}.firebaseio.com'
});
I also saw this question, creating new Google Cloud project from current Firebase project and upgrading Firebase project plan to Blaze Plan fixed the problem.
Use Case
I am using Firebase JS SDK to access Cloud Firestore from Browser, so that I can push UI Logs and Errors into Firestore.
Issue
It was all working fine until I, accidentally, deleted most of the Service Accounts in my Google Cloud Platform inside APIs & Services → Credentials.
Below is the sample Config that we use for a Firebase Web App:
<script src="https://www.gstatic.com/firebasejs/7.13.1/firebase-app.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyCztfcT6k6yXXXXXXXXXXXXXXXXXX",
authDomain: "<some-domain>.firebaseapp.com",
databaseURL: "https://<some-domain>.firebaseio.com",
projectId: "<some-domain>",
storageBucket: "<some-domain>.appspot.com",
messagingSenderId: "877458876543",
appId: "1:877458876543:web:9a9287dee234cd655ab7f2"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
I even tried reading Firebase Service Accounts Overview, but could not decide which Service Account to use for Firestore. Then, I tried adding different Service Accounts according to my Project ID and giving them Owner permissions to my Project, but it didn't work.
Full Error I am getting in Browser's Console:
Uncaught (in promise) FirebaseError: Missing or insufficient permissions.
at new Hr (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:48219)
at https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:378673
at wr.<anonymous> (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:377569)
at Wt (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:15221)
at wr.S.dispatchEvent (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:16063)
at Er.ua (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:45322)
at nr.S.Fa (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:43229)
at Ge (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:21453)
at qe (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:20854)
at Me.S.Ja (https://www.gstatic.com/firebasejs/7.8.2/firebase-firestore.js:1:23264)
Actually, I want to cleanup my Service Accounts as there were many created by myself. Please guide in telling which Service Account to use and what suitable Permissions to give.
Firestore Security Rules depend on a service account named service-PROJECT_NUMBER#firebase-rules.iam.gserviceaccount.com with the role
roles/firebaserules.system. You can use gcloud to restore this account:
gcloud projects add-iam-policy-binding PROJECT_ID --member=serviceAccount:service-PROJECT_NUMBER#firebase-rules.iam.gserviceaccount.com --role=roles/firebaserules.system
To get the project number, see Identifying projects.
Taking my first steps with Firebase. Just a few pointers to documents explaining what is going on will help.
I have set up a new firebase web project and connected my dev environment with the firebase sdk. Now when I drop the email-password quickstart into the project it works. (after enabling email/password sign in.)
But wait: This quickstart html file does not contain any configuration that the firebase console tells me to paste:
var config = {
apiKey: "xxx",
authDomain: "xxx.xxx.com",
databaseURL: "https://xxx.xxx.com",
projectId: "xxxx",
storageBucket: "xxx.xxx.com",
messagingSenderId: "xxx"
};
Does the local firebase server add the configuration in the background? When and where do I have to add this configuration to web pages?
If you check line 35 of that quickstart, you'll find this magic include:
<script src="/__/firebase/init.js"></script>
This is an auto-generated file by Firebase Hosting, that contains precisely the code that initializes the connection to the Firebase backend services of your project.
If you'd like to learn more about it, read:
Easier configuration for Firebase on the web
SDK imports and automatic initialization
SDK auto-configuration
Using Google Firebase in my ios swift app, I found the infamous message in my console output:
App Transport Security has blocked a cleartext HTTP (http://) resource
load since it is insecure. Temporary exceptions can be configured via
your app's Info.plist file.
Using the method here, I was able to find that this is triggered by a request to load http://www.google-analytics.com/ga.js, which is presumably from Firebase Analytics.
Do I need to add an exception for this?
I'm using the firebase/analytics module (firebase 7.1.0) inside a react app using firebase hosting and I was getting an error trying to load the googletagmanager (which I am NOT doing explicitly, it is coming from the module).
Mixed Content: The page at 'https://example.com/' was loaded over HTTPS, but requested an insecure script 'http://www.googletagmanager.com/gtag/js?id=someGTM-id'. This request has been blocked; the content must be served over HTTPS.
I am not importing google analytics from inside the index.html file, rather I am loading firebase/analytics from a javascript file with basically:
import firebase from 'firebase/app';
import 'firebase/analytics';
const firebaseConfig = {
apiKey: "api-key",
authDomain: "project-id.firebaseapp.com",
databaseUrl: "https://project-id.firebaseio.com",
projectId: "project-id",
storageBucket: "project-id.appspot.com",
messagingSenderId: "sender-id",
appId: "app-id",
measurementId: "G-measurement-id",
};
firebase.initializeApp(firebaseConfig);
firebase.analytics();
I found: https://developers.google.com/web/fundamentals/security/prevent-mixed-content/fixing-mixed-content
and https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests
And fixed it by placing the following line inside my index.html file:
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
http://www.google-analytics.com/ga.js is not used by Firebase. Perhaps one of the other SDKs you are using accesses this script.
You do if you wish to use Firebase Analytics. Alternatively, you can disable analytics in the Firebase plist file.