DEV Community

Cover image for Firebase as simple database to React app

Firebase as simple database to React app

Nathan Sebastian on February 13, 2019

Firebase is an all-in-one backend as a service provider (BaaS) that provides database, authentication, cloud storage among their many services. In ...
Collapse
 
andy_preston profile image
Andy Preston

Excuse me if I've missed something. But surely having your API key inside the create-react-app will expose your backend to abuse.

Someone with knowledge of JavaScript could easily find the API key within your code and use the key to write whatever they want to the database?

Collapse
 
codewithnathan profile image
Nathan Sebastian

Oh none at all, you're right on point. I've also wondered the same thing until I came to this post:

javebratt.com/hide-firebase-api/

I won't put anything into my Firebase project except for this tutorial, so for all purpose I think it will be fine.

Collapse
 
jhuleatt profile image
Jeff

It is safe to include the config in your app. The Firebase docs address this:

A Firebase config file or config object associates your app with your Firebase project and its resources (databases, storage buckets, etc.).

The content is considered public, including your platform-specific ID (entered in the Firebase console setup workflow) and values that are specific to your Firebase project, like your API Key, Realtime Database URL, and Storage bucket name. Given this, use security rules to protect your data and files in Realtime Database, Cloud Firestore, and Cloud Storage.

Collapse
 
rolu315 profile image
Robert

I ran into an issue with Firebase to where whenever my e.preventDefault() is right after my onSubmit handler, it will not add data to my db in prod. It works fine in development which is odd. Check out what I wrote:

onSubmit = e => {
e.preventDefault();
const { fullName, email, subject, message, error } = this.state;
let contactID = email;
db.collection("formContacts")
.doc(contactID)
.set({
fullName: fullName,
email: email,
subject: subject,
message: message
})
.then(
(window.location = "/formSuccess").this
.setstate({ ...INITIAL_STATE })
.catch(
(window.location = "/formError").this.setstate({ ...INITIAL_STATE })
)
);
};

Any thoughts?

Collapse
 
codewithnathan profile image
Nathan Sebastian • Edited

Hello Robert, sorry for late reply. I don't know what's wrong by simply looking at your code. There may be several causes for it. I recommend you try these suggestions:
Does the catch call ever run when you do it?
Or maybe you had the wrong credentials for production environment?

If you don't have any React error in the console, most likely something is wrong in the process of saving your data to Firebase.

Hope this helps!

Collapse
 
ethanklein profile image
Ethan Klein

Thanks for the clear tutorial Nathan.
Is it possible to connect a front-end-only react app to firebase WITHOUT using "test" mode. I've been struggling to successfully connect my react apps to firestore other than through "test" mode. Any help would be greatly appreciated. Also, i'm fairly certain other people have similar concerns as there isn't much on stack overflow, reddit, etc. to answer this question

Collapse
 
mikakaakinen profile image
Mika Kaakinen

A good tutorial.

Collapse
 
dylanesque profile image
Michael Caveney

Very handy, thanks for taking the time to write this up!

Collapse
 
annlin profile image
ann lin

NOICE

Collapse
 
edisonpappi profile image
Edison Augusthy

waiting for new posts

Collapse
 
hemantp09549998 profile image
Hemant Patil

i am getting error while putting the mine keys of firebase project, so how to solve it?

Collapse
 
codewithnathan profile image
Nathan Sebastian

What error did you see in the console? It might be something with the security rule

Collapse
 
echoeyecodes profile image
EchoEye

Good tutorial and very useful. It works well in develop mode but during production build, it says something like:

"Reference error: IDBIndex is not defined" and cancels the build

Collapse
 
sandordargo profile image
Sandor Dargo

Thanks for the article!

Is this going to be a series? Are you planning to write about Firebase as an authenticator?

Collapse
 
codewithnathan profile image
Nathan Sebastian

Thank you Sandor! I actually haven't taught about that.. you just give me a new idea! I will try to come up with a more advanced Firebase integration. Stay tuned!