Use Firebase Security Rules, It allows you to allow or disallow access to your database, It's very easy, and part of Firebase:
As per Firebase:
Firebase Security Rules stand between your data and malicious users. You can write simple or complex rules that protect your app's data to the level of granularity that your specific app requires. Firebase Security Rules leverage extensible, flexible configuration languages to define what data your users can access for Realtime Database, Cloud Firestore, and Cloud Storage. Firebase Realtime Database Rules leverage JSON in rule definitions, while Cloud Firestore Security Rules and Firebase Security Rules for Cloud Storage leverage a unique language built to accommodate more complex rules-specific structures.
Here's an example for Firestore:
service <<name>> {
// Specify your resource path.
match <<path>> {
// Allow the request if the following conditions are true.
allow read, write: if <<condition>>
}
}
... but for Realtime Database, It's JSON based:
{
"rules": {
"<<path>>": {
// Allow the request if the condition for each method is true.
".read": <<condition>>,
".write": <<condition>>
}
}
}
Read more on https://firebase.google.com/docs/rules