using the Android Firebase SDKs I can define multiple FirebaseAuth instances associated with a Firebase app as shown below.
My question is: which auth instance will be used to perform Firebase db queries? Is is always the one that last signed in? How can I control which instance will be used?
Thanks!
String uri = "http://firebasedb...";
FirebaseApp app = ...
FirebaseAuth auth1 = FirebaseAuth.getInstance(app);
auth1.signInWithCustomToken(customToken1);
FirebaseAuth auth2 = FirebaseAuth.getInstance(app);
auth2.signInWithCustomToken(customToken2);
DatabaseReference dbRef = FirebaseDatabase.getInstance(app, uri).getReference();
FirebaseAuth.getInstance(app) will return you the same instance no matter how many times it will be invoked, and it will be the instance that will be used to signed in most recently.
Thus, auth1 and auth2 will be referring to same instance, which will be onward used to query database.
Related
I dont know what is this thing is called but i want to get a refrence of it in android so i want to know what exactly is this thing is called , please check the image down
im taking about this line above "Quote" , whaat is this line is called and how do i get a databse refrence of it in android and really sorry for that bad handwriting though
right now i want to get refrence of 'Quote" im doing it like this
databaseReference = FirebaseDatabase.getInstance().getReference("Quotes");
but how can i get a refrence of that line above"Quote"
databaseReference = FirebaseDatabase.getInstance().getReference("What should i put here to get that refrence ?");
Seems you are using realtime database that is called firebase reference url
you can try as:
import { getDatabase, ref, child, get } from "firebase/database";
const dbRef = ref(getDatabase());
get(child(dbRef, `users/${userId}`)).then((snapshot) => {
if (snapshot.exists()) {
console.log(snapshot.val());
} else {
console.log("No data available");
}
}).catch((error) => {
console.error(error);
});
You can find your Realtime Database URL in the Realtime Database section of the Firebase console.
Depending on the location of the database, the database URL will be in one of the following forms: https:// DATABASE_NAME . firebaseio.com (for databases in us-central1 ) for more details on it you can access the documentation on
https://firebase.google.com/docs/database/web/read-and-write
If you want to get a reference to the root of the database, you can call getReference without any value:
rootReference = FirebaseDatabase.getInstance().getReference();
Also see the documentation for getReference() without parameters, which says:
public DatabaseReference getReference ()
Gets a DatabaseReference for the database root node.
Returns: A DatabaseReference pointing to the root node.
I was using a firebase database in my Android app. Now I need to use another database but I want to know for the old versions of my app what will "firebaseDatabase.getInstance" return? Which database from my databases will be returned?
FirebaseDatabase.getInstance();
.getInstance() function actually gets a string of url as an input parameter. You can see it here.
Alternatively you can follow the tutorial here to actually create more than one instance in one app with manually setting FirebaseOptions, FirebaseApp and FirebaseDatabase objects.
// Manually configure Firebase Options
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("1:27992087142:android:ce3b6448250083d1") // Required for Analytics.
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw") // Required for Auth.
.setDatabaseUrl("https://myproject.firebaseio.com") // Required for RTDB.
.build();
// Initialize with secondary app.
FirebaseApp.initializeApp(this /* Context */, options, "secondary");
// Retrieve secondary app.
FirebaseApp secondary = FirebaseApp.getInstance("secondary");
// Get the database for the other app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);
Please also read the notes in the end, as using different databases might confuse your Google Analytics, causing analytics data drops.
I have the following document
and I have set the following rules
Every time I try to read the document using the Android SDK
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users").get().addOnCompleteListener(appExecutors
.networkIO(),
task -> {});
I am getting this error
"com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions."
I am not sure what is wrong is it the rules or the Android call.
It looks like you meant to type resource.data.author_id instead of resource.data.author_d in the rule.
This code is trying to access every document in the entire users collection:
db.collection("users").get()
If there any any document in that collection that violates the security rules you set up, then it will fail.
Did you instead intend to try to access the single document that the user is supposed to have access to?
I think you can fix this from firebase consol. You just need to sllow access to all user. As I remember by default there was allowed only authorized users.
It seems that I had to add a where close in my code
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseFirestore.setLoggingEnabled(true);
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users").whereEqualTo("authorId",user.getUid()).get().addOnCompleteListener(appExecutors.networkIO(),task -> {});
I'm trying to have one project in Firebase that will be responsible for one common thing that all Apps.
That is, I want to create Apps, then have these Apps access a particular Firebase Database of a project.
Looking at the Firebase Android docs, I can't find a way to send data to another firebase database in another project using the following, but where reference is of another project.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("example");
ref.push().setValue(d).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
finish();
}
});
You'll need to initialize a second FirebaseApp object with explicit options in your code:
FirebaseOptions options = new FirebaseOptions.Builder()
.setApiKey("AI...j0")
.setApplicationId("1:5...e0")
.setDatabaseUrl("https://myapp.firebaseio.com")
.build();
FirebaseApp secondApp = FirebaseApp.initializeApp(getApplicationContext(), options, "second app");
FirebaseDatabase secondDatabase = FirebaseDatabase.getInstance(secondApp);
secondDatabase.getReference().setValue(ServerValue.TIMESTAMP);
I got the configuration values from the second project's google-services.json. The API Key is under a property called api_key, the Application ID came from a property called mobilesdk_app_id and the database URL came from a property called firebase_url.
Also see the documentation on using multiple projects in your application.
This question already has answers here:
Best way to handle Firebase references with Android
(3 answers)
Closed 6 years ago.
I'm extremely new to Firebase. Naively, I would imagine it's ok to keep instantiating new Firebase refs that point to the same location and using them interchangeably.
However, I'm not sure if there are any subtleties around caching or other considerations I should be aware of that make reusing the same reference for the same Firebase location desireable/preferable over creating a new one.
Thank you.
Reuse old firebase ref instance when:
if you only refer to one filebase app url during get/set data to server progress.
Ex:
public class myActivity extends AppCompatActivity{
private Firebase firebaseRef; //declare unique refer instance for all code
#Override
protected void onCreate(Bundle bundle){
super(bundle);
//other code here......
Firebase.setAndroidContext(this);
firebaseRef = new Firebase("http://mydb.firebaseio.com/node");
}
}
Create new firebase ref instance when:
if use have plan to access to multi firebase app or multi nodes url then you can create a firebase instance at anytime, anywhare you want ref to them.
Ex:
Firebase firebaseFrefA = new Firebase("http://mydb.firebaseio.com/a");
Firebase firebaseFrefB = new Firebase("http://mydb.firebaseio.com/b");
Firebase firebaseFrefC = new Firebase("http://mydb.firebaseio.com/c");
Firebase firebaseFrefAU = new Firebase("http://mydb.firebaseio.com/a/u");
Note: firebase automaticly control all own's instance so you can free create new instance to use.