I'm accessing a firestone db on firebase through react js. Here is my firebase.js
file:
import firebase from "firebase";
const firebaseConfig = {
apiKey: "SECR",
authDomain: "SECRET",
projectId: "SECRET",
storageBucket: "SECRET",
messagingSenderId: "SECRET",
appId: "SECRET",
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
const auth = firebase.auth();
const provider = new firebase.auth.GoogleAuthProvider();
export { auth, provider, db };
Now if I understand correctly, this code just tells firebase the location of my db. Does this file also serve as authentication?
Let me clarify, Here I'm now writing to the db (this is on another file):
import { db } from "../firebase";
function Login() {
const handleLogin = () => {
const name = prompt("Type name");
if (name) {
db.collection("opponents").add({
name: name,
});
}
};
Here I'm able to access the db when my security rules in firebase are set to allow read, write from everyone. Which isn't secure. I want to change that.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
So I replaced the rules to this (and changed nothing else):
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
This time it didn't work. So my question is, how do I securely give ONLY my code read/write access to the db. Can I make it so that if my firebase config values are correct, then it gives me access? Or is there a better way to only make my code allowed to access the db?