This question already has answers here:
How to redirect multiple types of users to their respective Activities?
(3 answers)
Closed 3 years ago.
I'm creating an android application, my app contains two types of user accounts linked with firebase, when the users log in to their accounts, the first user(teacher) should be carried to a different activity than the second (student) user.
I tried the if statements but I don't know how to distinguish between the teacher and student account.
You need to store the type of user when creating the account. Try to store that data in Firestore or Realtime Database.
XXXXXXXXXXXXXXX:{
date:"2019-07-16",
id:"XXXXXXXXXXXXXXX",
name:"Teacher1",
type: "teacher"
}
Fetch and identify the type of user at the time of login. Then you can navigate them.
You can create a userType value in the database. Then you check this value at the input and direct it accordingly.
For example if the logged-in user is admin, I wrote a code so that the button appears:
if (user.getAdmin().trim().equals("true")) {
cardDesingHolder.isThatAdmin.setText("Admin");
cardDesingHolder.Admin.setVisibility(View.VISIBLE);
cardDesingHolder.Admin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mContext.startActivity(new Intent(mContext, AdminPanel.class));
}
});
} else {
cardDesingHolder.isThatAdmin.setVisibility(View.INVISIBLE);
cardDesingHolder.Admin.setVisibility(View.INVISIBLE);
}
There is a part called admin in my database. If this value is true, the button appears, otherwise it does not. You can do something like that.
My Database Example : http://prntscr.com/opxm9e
Related
This question already has answers here:
How to sort Firebase data according to Time Created?
(1 answer)
Firebase - Get Random Child From Database Node
(1 answer)
Closed 1 year ago.
I'm trying to show the user profiles of various users like tinder but I was wondering will the firebase always shows the user who registered first to other new users or if the iteration is random which will guarantee that the result will be random and everyone chances of getting displayed are equal?
If not does someone knows how to make it random in android?
How I'm doing it is
FirebaseDatabase.getInstance().getReference().child("Users").child(OppositeUserSex);
Edit
This is the code where I think sorting wouldn't help because I want it to be random
if(mAuth.getCurrentUser().isEmailVerified())
{
FirebaseDatabase.getInstance().getReference().child("Users").child(UserSex).child(currentUserID).child("isVerified").setValue(true);
}
DatabaseReference oppositeSexDB= FirebaseDatabase.getInstance().getReference().child("Users").child(OppositeUserSex);
System.out.println(OppositeUserSex + "oppositeUser");
oppositeSexDB.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull #NotNull DataSnapshot snapshot, #Nullable #org.jetbrains.annotations.Nullable String previousChildName) {
if(snapshot.exists())
{
if(!snapshot.child("Selection").child("Nah").hasChild(currentUserID)) {
if(snapshot.child("Url").exists() && snapshot.child("isVerified").exists()) { //If user has image only then show them else dont display them && if they are verified
items.add(new ItemModel(snapshot.child("Url").getValue().toString(), snapshot.child("Name").getValue().toString(), "", "", snapshot.getKey()));
}
adapter.notifyDataSetChanged();
}
}
}
Firebase doesn't guarantee that unless you enforce it with the query you make with "orderBy" and also need to specify the direction of sorting / ordering there.
Please have a look here: Order and Limit data with Firebase
So, basically pull the data the way you want it (which would save processing in Android. And if you cannot fully achieve it in query, then you may have to do the additional filtering / ordering in Android code (Java / Kotlin).
Please don't hesitate to ask if you have any more doubts on this. Thanks.
To my experience it shows in the order of when the data was inserted, but I wouldn't rely on that, I always define the fields I will use for sorting and retrieve sorted data based using them.
I bookmarked this video sometime ago, it might have all the answers you need
Sort and Filter Firebase Data
I am using the Fireabse real-time database with Android Studio to make an app. In the app, I wanted to make the user able to bookmark (or follow) a specific item (a sports game in my case).
The data for the sports game looks something like:
{
Games: {
Game A: {
Team A: A,
Team B: B,
Score A: 0,
Score B: 0,
isFollowed: false
}
}
}
Something like that. In my app, I changed the color of a Star icon whenever isFollowed is true. However, this is global, so it will be applied for other users too.
My question is, how can I personalize the isFollowed data and make it different for all users?
My current idea is to put a HashMap that has the unique key Id of the user as the key value and the boolean value as the value inside the 'isFollowed` data.
Is this a valid idea?
If not, please tell me if there is a better idea for doing this.
I am creating an android application using the Firebase authentication and database.
For my app I need to identify the users after they logged in and redirect them to two different activities. (admin/simple user)
In my app I have only 10-20 users all time, and there isn't a way to register, it's a company only app.
Thanks for help.
If max users you expect is 20 then why would you need a authentication usage ( Google, Facebook ) etc. Have a simple database entry for all users as follows:
{
"app_title": "YourApp",
"users": {
"-KTYWvZG4Qn9ZYTc47O6": {
"username": "pkondedath",
"useremail": "hello#kondedath.com",
"password" : "hello123"
"userpic": "example.com/a.png",
"prevelidge" : "superuser"
},
"-KTYWvZG466ADFRR667": {
"username": "someone",
"useremail": "hello2#kondedath.com",
"password" : "hello1234"
"userpic": "example.com/b.png",
"prevelidge" : "normal"
}
}
}
Provide a user to input username( unique) and password and search through database to see if username and password matches and check his/her prevelidge and redirect to particular activity appropriately.
eg
mFirebaseRef = new Firebase("https://yourapp.firebaseio.com/").child("users")
Use above database reference to search users. You can also use the same to add new user. When adding a new user, give them default prevelidge.
This model fits well for less userbase like 20.
For more user bases( thousands, millions) you can use google authentication and use the User UID returned and create a new entry in your database(users).
Hope this helps you.
This question already has answers here:
Good way to replace invalid characters in firebase keys?
(4 answers)
Closed 4 years ago.
I am working on an Android App where I want a Firebase database like
Basically, I want to verify email before creating a user on the app. Whenever a user registers in my app, First it will be checked whether the email address is available in the users database reference. if available, then only he is allowed to register.
But I found that Firebase doesn't allow an email as a child. So How can I do it? Any other suggestion for achieving this in Firebase Database.
Because Firebase does not allow symbols as . in the key, I suggest you encode the email address like this:
name#email.com -> name#email,com
As you probably see, instead of . I have used ,. To achieve this, you can use the following methods:
static String encodeUserEmail(String userEmail) {
return userEmail.replace(".", ",");
}
static String decodeUserEmail(String userEmail) {
return userEmail.replace(",", ".");
}
i have created my app, in which I have created several users. The challenge is, I need to construct the app in a way that, while I am logged in as a user (say User A), I can add input an object into another user (say User B). So, while am logged in as user A, I can input the data that will be saved into one of the empty field, of User B. Can you assist which way, I can arrange this? In short, how can I create an object to associate with the User B.
Here is the thing, while I am logged in as User A, I want to add "Scores" for both user A and B. for instance, if the user A's score is 7, and B's score is 20, I could add the scores while logged in the activity A, and they will be added in the field "Scores" of the corresponding users.
you should go for parse cloud code which is written in java script
refer this link to know more
https://parse.com/docs/js/guide#cloud-code
then you just edit main.js
deploy and call that cloud code from your code
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("updateTable", function(request, response) {
var currentUserId=request.params.currentLoggedinUser;
var userToBeUpdatedId=request.params.userToBeUpdated;
var User = Parse.Object.extend('_User'),
user = new User({ objectId: userToBeUpdatedId });
user.addUnique("followers",currentUserId);
Parse.Cloud.useMasterKey();
user.save().then(function(user) {
response.success(user);
}, function(error) {
response.error(error)
});
});
deploy this code using comand "parse deploy" from terminal.
call this code from your code like
ParseCloud.callFunction("updateTable", params);
i hope it will help.
The way that Parse is setup by default is so that only the current users data can be editted in the User table. You could probably change the settings for the table however, this could cause security issues.
What I would suggest would be to create a 'Scores' tables. There you could have a row for each user. with a 'userID' column which contained the objectId of the user to reference the user. You could then have a score column which you could update whenever you wanted.
Alternatively to storing the objectId of the user in the userID column you could make an association between the Scores row and the user. See the Parse documentation for how to create associations. https://parse.com/docs/android_guide#users-associations