Let me explain the situation (excuse my english, I will do my best):
I have two Firebase Web projects in my Firebase console: coretechtest-ce207 and agon-plugin
coretechtest-ce207 is the main app and agon-plugin is a secondary app wich needs to connect to the auth and database of coretechtest-ce207. As far as I know I can't host two apps on the same project so thats why I made to separated projects. The main one works fine, I can do everything I want (signup, database, etc.) but I need the main and the second one both on the same auth and DB. agon-plugin (secondary one) is made based on the FriendlyChat app and connects directly to the server in wich the app is hosted.
For example:
// Initializes FriendlyChat.
function FriendlyChat() {
this.initFirebase();
}
// Sets up shortcuts to Firebase features and initiate firebase auth.
FriendlyChat.prototype.initFirebase = async function() {
// Shortcuts to Firebase SDK features.
this.auth = firebase.auth();
this.database = firebase.database();
this.storage = firebase.storage();
// Initiates Firebase auth and listen to auth state changes.
await this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));
};
As you can see there is no need to put
apiKey: "AIzaSyAfGm_ILVdfsd--Fw7aascc8tAB73q__Bbko",
authDomain: "coretechtest-ce207.firebaseapp.com",
databaseURL: "https://coretechtest-ce207.firebaseio.com",
projectId: "coretechtest-ce207",
storageBucket: "coretechtest-ce207.appspot.com",
messagingSenderId: "994718782"
I tryied replacing it using those parameters so it would be
FriendlyChat.prototype.initFirebase = async function() {
// Shortcuts to Firebase SDK features.
this.auth = 'coretechtest-ce207.firebaseapp.com';
this.database = 'https://coretechtest-ce207.firebaseio.com';
this.storage = 'coretechtest-ce207.appspot.com';
// Initiates Firebase auth and listen to auth state changes.
await this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));
};
But no luck there, can you tell me what I am doing wrong? I thought that replacing that would connect to my main project but it did not... =/
I hope you can understand what Im trying to say!
Thanks!
Not 100% sure about Web, but it should be similar to Android :
Going to your console panel
On top left click on the wheel next to "Project OverView"
In the pop-up click on Project Setting
In the settings page scroll down a little bit and you should see a blue "add app" (or something like that) button.
Then do everything you did on your first app to your second app
Most importantly, dont post your api key on the web !
If you're using Web and want to host 2 webapps that use the same database - you have a couple options.
Free: the only thing that associates a web app to your db is the api keys in the Firebase config used to initialize firebase in the web app. If 2 apps use those same settings then they can share the same db. If you are wanting to host both web apps with firebase for free - only 1 app can be hosted for free per project. However, you can easily create a 2nd new project and host your 2nd web app in that new project. But don't use the new web api settings from that new project inside your app. Instead use the same ones from your original project. 2 apps in 2 projects can use the same DB if they use the same API keys config. (only select firebase hosting when doing 'firebase init' from the cli for the 2nd app)
A paid option: is if you go to the Hosting page in your firebase console, and you scroll down to the bottom -you will see a card offering hosting more than 1 app in 1 project. It requires you to update to the pay-as-you-go blaze plan. Which may or may not cost you money.
Related
This question already has answers here:
How to send one to one message using Firebase Messaging
(5 answers)
Closed 4 months ago.
I have zero knowledge about firebase. I have recently learned how to push data into the realtime database:
I have created a Firebase project and included both my android app as well as website.
After that I have included these script src into my html page:
<script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.10.0/firebase-database-compat.js"></script>
In my javascript, I am initializing my firebase with all the config details fetched from my project settings:
const firebaseConfig = {
apiKey: "AIzaSyBIJmvWbzdexxvyMGS-u5LFTOPlgWE6AO4",
authDomain: "nfr-qtr-electric-billing.firebaseapp.com",
projectId: "nfr-qtr-electric-billing",
storageBucket: "nfr-qtr-electric-billing.appspot.com",
messagingSenderId: "123052283905",
databaseURL: "https://nfr-qtr-electric-billing-default-rtdb.firebaseio.com",
appId: "1:123052283905:web:f0fc4ef2165122e112c800"
};
firebase.initializeApp(firebaseConfig);
After that I am pushing some data into my realtime database which is in an array:
for(var i=0; i<arr.length; i++){
firebase.database().ref("emp_details/0/").push({
"Name":arr[i][0],
"Address":arr[i][1],
"Gender":arr[i][2]
})
}
Now what I want to do is, with each data being pushed into the database, a notification should be sent to the android phone. I have no idea, as to how to proceed. I have followed all the firebase documentation pages, but they are way too complicated to understand.
In my android app, I have created a Firebase messaging service class which should check for incoming notification messages and should show them:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(#NonNull RemoteMessage remoteMessage) {
//How do I show the notification here?
}
}
Can anyone please help me out?
If you want to use the Firebase Cloud Messaging (FCM) service to send the notifications you need to "build message requests in a trusted server environment that supports the Firebase Admin SDK or the FCM server protocols" as explained in the FCM documentation.
This trusted server environment can be a server you manage or Cloud Functions for Firebase, the "serverless framework that lets you automatically run backend code in response to events triggered by Firebase features".
The documentation gives all the details to build message requests and send them.
So I have a single page frontend only app. Right now I have something like this
// db.js
import firebase from "firebase/app"
import "firebase/firestore";
var firebaseConfig = {
...
};
export const db = firebase
.initializeApp(firebaseConfig)
.firestore();
in main.js I was experimenting with putting the db instance in the global window scope just to see if I could go to the chrome web console and access it to submit a doc and indeed I can
// main.js
import { db } from './db'
window.db = db;
and then from chrome console
db.collection("test").add({'somekey': 'Can I add this doc?'})
How do I prevent someone from doing this without having a real backend to check auth? I like the reactivity of vue + firebase. If I don't expose the db variable to global scope is that enough? I was reading this post:
https://forum.vuejs.org/t/how-to-access-vue-from-chrome-console/3606/2
because any variable you create inside your main.js fiel will still not be globally available due to how webpack
One of the great things about Firestore is that you can access it directly from within your web page. That means that within that web page, you must have all configuration data to find the relevant Google servers, and find your Firebase project on those servers. In your example, that data is part of firebaseConfig.
Since you app needs this configuration, any malicious user can also get this data from your app. There is no way to hide this: if you app needs, a sufficiently motivated malicious user will be able to find it. And once someone has the configuration, they can use it to access your database.
The way to control access to the database, is by using Firebase's server-side security rules. Since these are enforced on the server, there is no way to bypass them, neither by your code, nor by the code that a malicious user writes.
You can use these security rules to ensure that all data is valid, for example making sure that all the required fields are there, and that there's no data that your app doesn't use.
But the common approach is to also ensure that all data access is authorized. This requires that your users are authenticated with Firebase Authentication. You can either require your users to sign in with their credentials, or you can anonymously sign them in. In the latter case they don't need to enter any credentials, but you can still ensure for example that each user can only write data to their own area of the data, and that they can only read their own data.
I would like a Google Cloud project A (project-a-id) to access the firestore data of another Google Cloud project B (project-b-id). For the same I added project A default service account viz. project-a-id#appspot.gserviceaccount.com in the IAM of project B and set the role to Cloud Filestore Editor.
In the cloud function of project A, I am trying to access both project A's (its own) firestore as well as project B's firestore but it keeps showing project A default database for both Apps. The code is:
var primaryAppConfig = {
databaseURL: 'https://project-a-id.firebaseio.com'
};
var primaryApp = admin.initializeApp(primaryAppConfig, 'primary');
var primarydb = admin.firestore(primaryApp);
var secondaryAppConfig = {
databaseURL: 'https://project-b-id.firebaseio.com'
};
var secondaryApp = admin.initializeApp(secondaryAppConfig, 'secondary');
var secondarydb = admin.firestore(secondaryApp);
I was under the impression if the default service account of project-a is given rights in project-b it should automatically get rights. At least I found it applicable when I am accessing google cloud storage buckets in this manner.
Is something else to be done? Thanks
I have a cloud-native firestore as opposed to a real-time database in project-a. However, was facing the same issue when I tried to access it from project-b.
Was able to solve it by generating a service account with access to project-a firestore, downloading the credentials and accessing the same from project-b with the following:
credential_path = "pathTo/xxxxx.json"
db = firestore.Client.from_service_account_json(credential_path)
You need to create apps within current firebase project.
Firebase Console -> Project Setting -> General -> Add App
These apps will have access to same firestore but will be deployed seperately under different subdomains(under firebaseapp.com).
I'm trying to do server side rendering with Firebase and am struggling with using the Admin SDK to impersonate a uid so that all database security rules are followed as if the request came from an authenticated user with the same uid. I'm aware of the databaseAuthVariableOverride functionality in initializeApp, but that only works for a single initialization, not a potentially enormous amount per second. Anyone have a totally different approach or suggestions for me?
Here's the equivalent of what I would like to do:
const getAdminAsUser = (authorizedUid) => firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(adminConfig),
databaseURL: firebaseConfig.databaseURL,
databaseAuthVariableOverride: {
uid: authorizedUid,
},
});
I have to make an offline application that sync with another online app when it can.
I developed the offline app using PouchDB. I create this app thanks to the github repo create-react-app. This app is running at localhost:3000. In this app, I create and manage a little DB named "patientDB".
I manage the db with the classical put method like we can see in the documentation:
var db = new PouchDB('patientDB')
db.put({
_id: 'dave#gmail.com',
name: 'David',
age: 69
});
With the development tool for chrome given by PouchDB, I can see that the DB is working like I want (the documents are created):
The other application is another React application with a node server. During the development, this app is running at localhost:8080.
In this app I try to fetch all the docs contained in the "patientDB" with the following code:
const db = new PouchDB('patientDB', { skip_setup: true });
db.info()
.then(() => {
console.log("DBFOUND")
db.allDocs({include_docs: true})
.then(function (result) {
console.log("RESULT" , result)
}).catch(function (err) {
console.log("NOPE")
console.log(err);
});
})
My problem is that I can't get the "patientDB" created with the offline app in the online app. When I do a var db = new PouchDB ('patientDB') it create a new and empty db because it can't find a db which is already present.
I use google chrome to run all my application so I thought that the dbs could be shared.
However I did little and very simple tests with two html files:
First.html which initialize a new db with a doc
Second.html which read the db create in First.html
In this case, I can fetch the doc created with First.html in Second.hmtl even if the are two separated "website".
I think that the application which run at a localhost are like isolated of the rest of the application even if, like I said before, I use the same browser for all my applications...
I don't know what to do or I don't know if it's even possible to do what I want to do. If someone has an idea for me, I would be pleased.
EDIT
I can see why my DBs are not shared:
When I look at all my local DBs after running an html file I can see the following thing :
As we can see, the DBs come from the files _pouch_DB_NAME - file://
When I check my DB from the application running localy (localhost), I can see this :
The DB don't come from file but from localhost:8080
If you know how I can fetch doc from a local db in an app running in a server, it could be really helpful for me!
PouchDB is using IndexedDB in the browser, which adheres to a same-origin policy. MDN says this:
IndexedDB adheres to a same-origin policy. An origin is the domain, application layer protocol, and port of a URL of the document where the script is being executed. Each origin has its own associated set of databases. Every database has a name that identifies it within an origin.
So you have to replicate your local database to a central server in order to share the data. This could be a PouchDB Server together with your node app. You can also access PouchDB Server directly from the browser:
var db = new PouchDB('http://localhost:5984/patientDB')
As an alternative, you can use CouchDB or IBM Cloudant (which is basically hosted CouchDB).