Firebase as release the functions V1.0 and it means a lot of changes in the way to write the code.
I would like to do the migration, I understood all the changes, except the one about the admin.initializeApp
Firebase says that
firebase-admin is now initialized without any parameters within the
Cloud Functions runtime
But I need to access another project database from my actual project.
Until now I was doing it as follows :
/* Initialize the SecondApp service acccount */
var serviceAccount = require("./service-account.json");
/* Initialize SecondApp with the serviceAccount credentials */
var SecondApp = admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
apiKey: "**************",
authDomain: "****************",
databaseURL: "****************",
projectId: "****************",
storageBucket: "****************",
messagingSenderId: "****************"},"SecondApp");
What will be the new way of doing it ?
Related
In my project I have setup a dev and production environment by doing this:
const firebaseConfig =
process.env.FUNCTIONS_EMULATOR === "true"
? {
apiKey: process.env.FIREBASE_DEV_KEY,
authDomain: "myapp-dev.firebaseapp.com",
projectId: "myapp-dev",
storageBucket: "myapp-dev.appspot.com",
messagingSenderId: "...",
appId: "...",
measurementId: "...",
}
: {
apiKey: process.env.FIREBASE_PROD_KEY,
authDomain: "myapp-production.firebaseapp.com",
projectId: "myapp-production",
storageBucket: "myapp-production.appspot.com",
messagingSenderId: "...",
appId: "...",
measurementId: "...",
}
admin.initializeApp(firebaseConfig)
Then firestore points to the right project because it was initialized from admin like above:
const firestore = admin.firestore()
However, I am now trying to listen to storage uploads.
And by default, it is listening to my production app. I need to listen to uploads to my dev project.
The problem arises since the storage listener is coming from functions and not admin (which was initialized with the right config).
How can I "initialize" the storage listener (below) so that it listens to the dev project?
exports.generateThumbnail = functions.storage
.object()
.onFinalize(async (object) => {})
Is there maybe a way to attach a listener to admin.storage()?
I'm not sure but it seems like you are using the Client SDK configuration in the Cloud function. The Admin SDK uses a service account instead of the client config.
The FIREBASE_CONFIG environment variable is included automatically in Cloud Functions for Firebase functions that were deployed via the Firebase CLI.
That being said, you just need to initialize the Admin SDK as shown below:
import * as admin from 'firebase-admin';
admin.initializeApp()
Talking about the environments, I can clearly see that you have two different projects for different environments. In that case you would have to deploy the functions to both the project separately because the Admin SDK which uses service account for the dev project will be listening for changes in the dev project only
Other option would be initializing the Admin SDK twice as mentioned in the documentation.
You can also consider using the same Firebase project for both dev and production but use different buckets for them. That way you can listen to those buckets separately. For listening to a specific bucket, specify it's name:
const devFunction = functions.storage.bucket('dev-bucket').object().onFinalize(async (object) => {
//...
})
const prodFunction = functions.storage.bucket('prod-bucket').object().onFinalize(async (object) => {
//...
})
in retool how to connect and upload the data in firebase using retool ,if any one help me out.
<script>
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
apiKey: "AIzaSyC0W4G9vFxKV99kYDPvw_qgoMOF6Yj720g",
authDomain: "test-b0e28.firebaseapp.com",
databaseURL: "https://test-b0e28-default-rtdb.firebaseio.com",
projectId: "test-b0e28",
storageBucket: "test-b0e28.appspot.com",
messagingSenderId: "729064532529",
appId: "1:729064532529:web:ab1263c616218ebb990310",
measurementId: "G-Y64XEPGK2V"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
Retool has a native integration with Firebase, so you use their GUI for querying instead of writing in the SDK directly. Here's an example of what it looks like to query Firestore:
To connect a data source, you go to yoursubdomain.retool.com/resources/new and then find Firebase.
I'm new to React Native and Firebase coding. I'd like to skip the login process if the user logged in successfully last time. According to Firebase doc, firebase.auth.Auth.Persistence.LOCAL can persist the authentication state even the activity is destroyed. The problem is how can I check the local persisted authentication state when the app initializes?
I managed to use AsyncStorage to achieve the goal, but firebase.auth.Auth.Persistence.LOCAL is supposed to be the simpler approach, but I can't figure it out.
Init codes:
<pre>
componentWillMount() {
const firebaseConfig = {
apiKey: "xxxxxxxxxx",
authDomain: "xxxxx.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
projectId: "xxxxxx",
storageBucket: "xxxxx.appspot.com",
messagingSenderId: "xxxxxxx"
};
if (!firebase.apps.length) {
console.log("call firebase init");
firebase.initializeApp(firebaseConfig);
console.log(firebase.app().options);
}
}
</pre>
I am able to trigger the push notifications, but i get this site has been updated in the background instead of the actual message.
The service worker code :
importScripts('https://www.gstatic.com/firebasejs/3.7.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.7.1/firebase-messaging.js');
// Initialize Firebase
var config = {
apiKey: "**************",
authDomain: "push-notifications-240e1.firebaseapp.com",
databaseURL: "https://push-notifications-240e1.firebaseio.com",
projectId: "push-notifications-240e1",
storageBucket: "push-notifications-240e1.appspot.com",
messagingSenderId: "********"
};
firebase.initializeApp(config);
i have fixed the issue, The service worker file had been cached. I will close this question now. I fixed it by adding the file in cache control.
This is my very basic Cloud Function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore()
exports.createdNewAccount = functions.auth.user().onCreate(event => {
return db.collection('users').doc(event.data.uid).update({
"creationDate" : Date.now()
})
})
And I get the error
Error: no entity to update: app
What is wrong with my code?
Most likely, the document for event.data.uid does not exist. The documentation for update() states:
The update will fail if applied to a document that does not exist.
Use set() instead.
I faced a similar error when testing my app locally with the Firebase emulator. My firebase config file looked like:
import firebase from "firebase";
import "firebase/firestore";
const firebaseConfig = {
apiKey: <FIREBASE_API_KEY>,
authDomain: <FIREBASE_AUTH_DOMAIN>,
databaseURL: <FIREBASE_DB_URL>,
projectId: <FIREBASE_PROJECT_ID>,
storageBucket: <FIREBASE_STORAGE_BUCKET>,
messagingSenderId: <FIREBASE_MSG_SENDER_ID>,
appId: <FIREBASE_APP_ID>,
measurementId: <FIREBASE_MEASUREMENT_ID>,
};
// Initialize Firebase
const firebaseApp = firebase.initializeApp(firebaseConfig);
// for local instances, use the emulator to connect the frontend to firestore & functions
if (location.hostname === "localhost") {
firebase.firestore().settings({
host: "localhost:8080",
ssl: false,
});
firebase.functions().useFunctionsEmulator("http://localhost:5001");
}
Turns out my local Firestore database (expected to be running at localhost:8080) wasn't being hit. So my cloud functions were trying to write to a non-existent db. The underlying issue was that I also had my backend initializing to a different database:
// functions/index.js
const admin = require("firebase-admin");
var serviceAccount = require("./serviceAccount.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://other-firebase-project-id.firebaseio.com",
});
The solution (originally adapted from a Fireship tutorial) was to remove this [incorrect] re-initialization of the database altogether:
// functions/index.js
...
admin.initializeApp();
...
After all, according to the docs, we can initialize the Firebase Admin SDK without parameters since the FIREBASE_CONFIG environment variable is included automatically in Cloud Functions for Firebase functions that are deployed via the Firebase CLI.
FWIW, also be sure to set the correct Firebase project on the CLI. Doing so with this allows the Firebase CLI to hook Functions/Firestore to the right project:
firebase use <desired-firebase-project-id>
If you have multiple Firebase projects, you can list them out with: firebase projects:list