I have two different types of users Teachers and Students . I use Firebase Auth with Email and password to Authenticate them and store them in the Firebase Real time database . My question is is there a way to create custom accesor methods such as getCurrentUser().getEmail, getDisplayname etc . I need to display different UI for different user type (Teacher/Student) from the current user-type
You got 2 type of users. So first of all, you can make only one firebase structure for both user and just add a boolean variable TEACHER that indicates if a user is a teacher or a student.
But what I usually do is to seperate the users in two different firebase structures meaning that you should create a firebase path teacher/user_id and an another student/user_id which will give you flexibility with retreiving data and display the data in different UI.
Related
So I am creating an Android application in which suppose there are 2 user types, userType1 & userType2. Now what I want is that only userType1 will see and be able to access the post button, and userType2 Can't see and or access it. So what is the best practice for that?
The best practice would be to implement Firebase Authentication and save user data, either in Cloud Firestore or in the Realtime Database together with the user type. When you start the app, first you have to read the user type and then display the UI elements according to it. I have answered a question in which I have explained how to redirect the user according to its type to the corresponding activity. Here is how you can do that using Cloud Firestore:
How to redirect a user to a specific activity in Cloud Firestore?
Or using the Firebase Realtime Database:
How to redirect multiple types of users to their respective Activities?
The same mechanism can be used to hide/display a button. Don't also forget to secure your data using security rules.
I wanna ask about the concept and logically ways to give another user the privilege to access other's users' data. What I want to do exactly is like this :
Basically, collection 1 contains several Users ID (UID) from authentication, then the user will have their own data collected in collection 2 which contain the data ID.
So, it's like giving access to another user to collaborate with the data like Google Docs Apps where we can add another user to edit our documents. I've been thinking of how to do this, but still, I got stuck.
My question is, how can I possibly do this? cause from what I've read, cloud firestore don't use such a foreign key like MySQL. Thank You
haven't tried something like this but i think this approch overcomes your problem.
modify your structure according to above image. userID collection will contain userIds which are allowed to edit their parent collection.and create firestore rules according to your use to check weather the userId is allowed to edit the Collection or not.
in your case when 'user 2' will have reference to 'collection 2', he/she will try to change data. firebase rule will check if auth.userId is inside the 'collection2.UserIDs' or not and will allow according that.
I have done phone authentication linked to my project but I am not pushing the data from android instead I have manually entered the user data in Firebase. And I want to retrieve the data from firebase using OTP verification and I just need to search the specific user data with phone number which I am entering while login. I want to show all fields to be displayed which are present in my firebase database into my android project.
I've tried many methods to receive the data but I am stuck... Please help me.
Database structure
Authentication panal
You need to logically bind your authenticated user and the data that you stored in your firebase. As per the image in your data base 'id' of the 'Users' are 1,2,.. like that. Instead of that you can use 'UID' of users as an an Id in your database. Go to your 'Authentication' section, you will see User UID against each registered user. Copy that and use as an ID while creating data in database. This will help you to get the data with reference like Users\. Hope this will solve your problem.
Ok, so in this scenario we have three users that download my app: Mike, Suzy, and John. I want to store an Entity object on the Datastore using this code, for each user:
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Entity e = new Entity("Person",someKey);
e.setProperty("name",name);
e.setProperty("age",age);
ds.put(e);
So when the three users named above use my app for the first time, my Google Cloud Datastore shows three entities of kind, "Person" with name properties: "Mike", "Suzy", and "John", and the cooresponding age properties.
Lets say that John opens the app and wants to see who else is using the app, how can I get those entities and display them to john so that he sees Suzy and Mike's names?
Basically, I want to be able to share data between all users by storing entities on the Datastore, and granting access to other users' entities to every user.
Another scenario could be an implementation of a High Scores list: Let's say I only want to keep the top 5 all time high scores across all users:
Entity e2 = new Entity("HiScore",aKey);
e.setProperty("score",score);
ds.put(e);
So when a user gets a new score, I would pull all existing entities, check the score properties on all of them and update the datastore accordingly by replacing a score. At the same time, I'd also want this data update pushed to other devices.
So here are two general examples along the lines of what I would like to accomplish (I know about google-play API that handles high scores for you, so don't tell me to use that because I'm not necessarily trying to implement high scores). In short I want to create a feed of the current entities that exist on my Datastore that can be seen by all users
Is using datastore entities the right way to go for this?
This is a very broad question (or set of questions), but to answer some of your individual points:
Lets say that John opens the app and wants to see who else is using the app, how can I get those entities and display them to john so that he sees Suzy and Mike's names?
That's a very simple query, like this:
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Query q = new Query("Person");
List<Entity> persons = ds.prepare(q).asList(FetchOptions.Builder.withDefaults());
You then say
I want to be able to share data between all users by storing entities on the Datastore, and granting access to other users' entities to every user
Users don't own entities; your Cloud Platform project owns entities. So it's up to your application to determine who can see what data. There's nothing inherent in Datastore preventing Suzy from seeing Mike's Person entity unless your application puts rules in place.
Finally,
Another scenario could be an implementation of a High Scores list: Let's say I only want to keep the top 5 all time high scores across all users...
... So when a user gets a new score, I would pull all existing entities, check the score properties on all of them and update the datastore accordingly by replacing a score.
Depending on your traffic you may find that solution doesn't scale well; I would recommend reading this case study.
I am new in android. I am developing android app and using http://parse.com API. I want to implement two types of user Student and Company. Basic signup form has the same fields. Later, I want to add additional information related to the user and specific for each type. Now, problem how to save user information for different type of users?
Make a column of userType in parse user table to differentiate user category.
And save user type during sign up and it's information.
I am also using this method because my application also has three end user.
This is working properly for me.
Hope this may helpful for you.