Firebase - Retrieve custom data by UID - android

So, I have a list with some users:
And a custom table with Username and a User UID reference:
After the user logged in I store his UID in a variable but, what is the correct way to retrieve the userName based on UserUID?

I recommend changing the structure of your database. Instead of using push() when you add users to the database, use setValue() with the userId as the key and the username as the value:
{
users: {
EsqhdtziWRPtoNLu485zvg: "Test User 1",
yBnNVVyHHcfq67ccGABGz2N8R: "Test User 2",
...,
...
}
}
This makes the username much easier to access.

Related

Firestore security rules to allow access only to specific queries and not the whole collection

Given the following simplified Firebase Firestore database structure:
users
user1
email: "test1#test.com"
user2
email: "test2#test.com"
I want to be able to query if a user with a specific email exists in the database WITHOUT giving access to the whole users collection
Is it possible to achieve this using the database rules without modifying the database structure?
If it's not possible, what would be the best workaround for this?
I see two possible solutions, but it seems to me that they add too much complexity:
Expose that specific query via an API endpoint (maybe using Firebase Functions)
Modify the DB structure as suggested in this thread: Firestore security rules based on request query value
Which approach do you think is better?
To meet the requirement, "query if a user with a specific email exists in the database WITHOUT giving access to the whole users collection," you'll need to rethink your database architecture. Firestore won't allow you to make queries on data that you don't also have read access to.
I would recommend creating a separate collection that just contains email addresses in use like this:
{
"emails": {
"jane#example.com": { userId: "abc123" },
"sally#example.com": { userId: "xyz987" },
"joe#example.com": { userId: "lmn456" }
}
}
This collection should be updated everytime a user is added or an email address is changed.
Then set up your Firestore rules like this:
service cloud.firestore {
match /databases/{database}/documents {
match /emails/{email} {
// Allow world-readable access if the email is guessed
allow get: if true;
// Prevent anyone from getting a list of emails
allow list: if false;
}
}
}
With all of that you can then securely allow for anonymous queries to check if an email exists without opening your kimono, so to speak.
List All Emails
firebase.firestore().collection('emails').get()
.then((results) => console.error("Email listing succeeded!"))
.catch((error) => console.log("Permission denied, your emails are safe."));
Result: "Permission denied, your emails are safe."
Check if joe#example.com exists
firebase.firestore().collection('emails').doc('joe#example.com').get()
.then((node) => console.log({id: node.id, ...node.data()}))
.catch((error) => console.error(error));
Result: {"id": "joe#example.com": userId: "lmn456"}
Check if sam#example.com exists
firebase.firestore().collection('emails').doc('sam#example.com').get()
.then((node) => console.log("sam#example.com exists!"))
.catch((error) => console.log("sam#example.com not found!"));
Result: sam#example.com not found!

How to add JSON object with specific Key name in Firebase Database Android

I have a application which works on Firebase Database.After successfull Login of a user created in Firebase using Firebase Auth user is saving some data in Firebase.The requirement is I need to add the data POST by that user with some key to identify data.
[
{
"key":"Satyam",
"value":"some data"
},
{
"key":"Akash",
"value":"some data"
},
{
"key":"Yash",
"value":"some data"
}
]
Suppose user Satyam has login using credentials of Firebase Auth,so data POST by Satyam need to be added in database where key is Satyam. How to achieve this ?

Get data from Firebase Database with Specific node after successfull Authentication

I have used FirebaseAuth for Login purpose in application. While createUserWithEmailAndPassword I am creating the same user in FirebaseDatabase with some additional fields.The structre is
[{
"email":"abc#gmail.com",
"password":"xyz",
"name":"sachin",
"address":"Pune",
"contact":"1234567890"
},
{
"email":"pqr#gmail.com",
"password":"def",
"name":"Ashay",
"address":"Pune",
"contact":"1234577777"
}]
Suppose after successfull Login of user Sachin with email abc#gmail.com and password xyz I want the address and contact from database.How to get that values ?
For future reference if you want to get a certain information from DB using UID
//To get logged in user information
var user = firebase.auth().currentUser;
var uid = user.uid; //get the UID from the current user
According to me, you should use firebase uid as index for storing data. When user authenticated you will get user's uid, on this basis you can access user's data. I hope it will work.

How to give unique usernames from a list of usernames to the users in the firebase

I want to give unique usernames to my users from the list of usernames that I have created. But I am not getting the logic of what should be done. I have tried uploading that as a JSON file in the Firebase realtime database but as every name is unique how to call it so that it will be assigned to the user
There is no concept of assigning a unique name to a user anywhere in Firebase, so anything you do will be specific to our app and use-case.
But a common approach would be to associate the UID of the Firebase Authentication user with the user name they selected. So:
usernames: {
"Sahil": "uidOfSahil",
"puf": "uidOfFrank"
}
You could have the unclaimed names in the same list, but have a marker value instead of the UID. For example if your marker value is false:
usernames: {
"Abe": false,
"James": false,
"Sahil": "uidOfSahil",
"Tony": false,
"puf": "uidOfFrank"
}

How to retrieve current user data using parse sdk in android?

The goal is : after a user has been logged in , the next activity should display his list of courses.
There is a table of courses in Parse's cloud, but how can I retrieve the correct courses (current user's courses) ?
I've thought of 2 approaches :
1. using getCurrentUser method - but what if there are more than one user in the cache ?
2. save for each course the userName that saved it , and then query for that userName - but how should I know that the currentUser method will return the correct user ?
How can I solve this problem ?
using getCurrentUser method - but what if there are more than one user in the cache ?
There's always one currently logged in user and ParseUser.getCurrentUser() will get it. Well, unless there is no logged in user.
but how should I know that the currentUser method will return the correct user
As above. I don't get what you're confused about.
There is a table of courses in Parse's cloud, but how can I retrieve the correct courses (current user's courses)
and
save for each course the userName that saved it , and then query for that userName - but how should I know that the currentUser method will return the correct user ?
I can think of two approaches here. There may be more. Up to you what you decide to use.
So you have the _User table and the Course table. You can do one of the following:
Add a courses array to each _User and for each _User, store their list of courses there. It should be a list of objectIds, so basically you'll have the ID's of all the courses that _User is registered for. Each Parse object has a limited size in the database so you should use it if you are sure you won't need big amounts of data for each user. If you want to retrieve this, query the _User table by the id of the user and fetch the list of courses.
The "relational" approach: add an UserCourse table that has a userId and a courseId column. Every time you want to add a course for a particular user, you add a new entry in here that holds the id of that user and the id o the course. Similarly to the above, have a query that fetches all the stuff from UserCourse by userId. Note that in this case you'll also need to make sure your query fetches the data from _User and Course. Otherwise you'll just end up with UserCourse entries, which merely store ID's.

Categories

Resources