Why does the email-password example work? - firebase

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

Related

Expo: cannot get getDevicePushTokenAsync due to non-valid Firebase project ID (Android)

I just started implementing push notifications in Expo - following the guides on expo.io. Hence, I created an Android app in my Firebase console and my app.json contains now a reference to the Google service file:
"android": {
"package": "------------",
"googleServicesFile": "./google-services.json",
"useNextNotificationsApi": true,
}
If I test this on a real android device (standalone app) I do not receive a valid token via this function:
token = (await Notifications.getExpoPushTokenAsync()).data;
Instead I get the following error:
Error: Encountered an exception while calling native method: Exception
occured while executing exported method getDevicePushTokenAsync on
module ExpoPushTokenManager: Please set your project ID. A valid
Firebase project ID is required to communicate with Firebase server
APIs: It identifies your project with Google."
I guess it has something to do with the way of my project setup. I also followed the Expo guides to setup firebase (e.g. for phone authentication) and hence created a web app in the Firebase console. In my application I initialize the Firebase web app with my web app configuration like this:
const FIREBASE_CONFIG: IFirebaseOptions = {
apiKey: '--------',
authDomain: --------.firebaseapp.com',
databaseURL: 'https://--------.firebaseio.com',
projectId: '--------',
storageBucket: '--------.appspot.com',
messagingSenderId: '--------',
appId: '--------',
measurementId: '',
trackingId: '',
};
firebase.initializeApp(FIREBASE_CONFIG);
The FIREBASE_CONFIG (for my Firebase Web App) as well as the google-services.json (for my Firebase Android App) have the correct project ID. Do I need to initialize the Firebase Android App somehow?
I am using Expo SDK 40.0.1.
What helped me: I deleted the old Firebase Android App, created a new one, downloaded the google-services.json file and built a new version of my app: It worked.
I cannot come up with an explanation why the first Firebase Android App was faulty because I did not make anything different than I did now.
Hopefully that will help some of you as well.
Hey I got this same issue with my react native project. Actually it was working fine but one day this error popped up so I checked the last working version of my files.
The actual issue was with the version of my google services in the gradle file.
Instead on 3.0.0, I have changed it to "classpath 'com.google.gms:google-services:4.3.8'".

How can I initialize the Firebase Admin SDK in spring-boot application using kotlin

I would like to get data from Firebase. I found some tutorials, but all is using Google Credential ServiceAccount to initialize firebase app. this's is tutorial that i found :
https://medium.com/techwasti/spring-boot-firebase-crud-b0afab27b26e , and else tutorial i found, almost like that too, is using ServiceAccount.
But the problem is i cannot get the ServiceAccount from this firebase because it's from third party. that's mean the owner of firebase don't want to give ServiceAccount.json to me, but they give me firebase config to me, like this :
const firebaseConfig = {
apiKey: "apkey",
authDomain: authdomain",
databaseURL: "databaseurl",
projectId: "projectid",
storageBucket: "storragebucket",
messagingSenderId: "messagesenderid",
appId: "appid",
measurementId: "measurementid"
};
So, how can i initialize this firebase using kotlin in my java springboot aplication?
i had try to find the solution, but not found anything,
I would be glad for any help.
If you want to initialize the Firebase Admin SDK, you will need a service account. There are no alternatives for this.
The config for the front end web app that you're showing here will not work at all. That config is only used for frontend apps that are acting as clients on behalf of the end user.

FirebaseError: Missing or insufficient permissions after deleting Google Service Accounts

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.

Using Same Firebase App With Web and Mobile App

So say I am building a web app using React and also a mobile app that allows users to take a photo using React Native. I want to allow users to use the same login credentials when signing into the web app and also the mobile app. Can I use the same Firebase info for both the web app and mobile app? Like just include the snippet below in both code bases to allow them to both use the same database, storage, etc?
<script src="https://www.gstatic.com/firebasejs/3.6.1/firebase.js"></script>
<script>
// Initialize Firebase
// TODO: Replace with your project's customized code snippet
var config = {
apiKey: "<API_KEY>",
authDomain: "<PROJECT_ID>.firebaseapp.com",
databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
storageBucket: "<BUCKET>.appspot.com",
messagingSenderId: "<SENDER_ID>",
};
firebase.initializeApp(config);
</script>
A single Firebase project can be used as the back-end for many (iOS, Web and Android) apps. All these apps will be talking to the same back-end services.
So if you create two web applications on a single Firebase project, those apps will be using the same database, the same list of users, the same stored files, etc. If you then add an iOS or Android app, that will also be accessing the same users/database/files.

How i version firebase code without credentials?

When you sign up to firebase they tell you to place this snippet in your code:
// Initialize Firebase
var config = {
apiKey: "your api key",
authDomain: "domain",
databaseURL: "database url",
storageBucket: "storage bucket",
messagingSenderId: "messaging sender id"
};
firebase.initializeApp(config);
Turns out i want to version it publicly on Github. Is there a way i can use some kind of environment variable or should i just don't version a credentials.js script.
All the information in that snippet is meant to be publicly shared with the users of your web app.
These are not secrets or credentials, but they're identifiers that allow the app code to find the Firebase project on Google's servers.
That said: many teams do not want to check it in to version control system, but instead force each developer to use their own Firebase project for development/testing.

Resources