Check for username's existence | Parse.com - android

I'm searching for a good way of querying usernames towards my app's user-database. So that people can't signup with the same username as someone else.
I'm using a username, password and an email for recover of password. So I want to check for username's basically.
I've understood this can be achieved with ParseQuery?
I would like an example if anyone can provide?

You can use a ParseQuery to query all usernames in the database like this.
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
// objects is a list of all the User you have. Just get all the usernames,
//and check if there's already one equal to the one you want to insert.
} else {
//error handling
}
}
}

Related

Query inside for loop in Back4App(Parse)

I am building a social network application using parse which have follow/unfollow feature.
I have two tables: Users and Follow table.
Users table have user related data and Follow table have two fields: followerId and followedId.
I have a list of searched users. So, corresponding to each user i just want to get from query that whether i am following him/her or not. So, how to run query inside a loop?
Any help will be deeply appreciated.
Thanks.
For get follower
ParseQuery query = new ParseQuery("FollowTable");
query.whereEqualTo("followedId", ParseUser.getCurrentUser().getObjectId());
query.findInBackground(new FindCallback() {
public void done( List<ParseObject> MyList, ParseException e) {
if (e == null) {
// Get your follower user list
}else{
//error
}
For get following
ParseQuery query = new ParseQuery("FollowTable");
query.whereEqualTo("followerId", ParseUser.getCurrentUser().getObjectId());
query.findInBackground(new FindCallback() {
public void done( List<ParseObject> MyList, ParseException e) {
if (e == null) {
// Get your following user list
}else{
//error
}
Then after call user list query in ParseUser table and get all the user.
Thanks

Get all users by Role Parse Android

Hi I have the next problem:
I have 10 users registered in Parse,a user can have the role of Admin or Player.
I'm trying to get all the users first but when I tried, the query only return one (The current user).
How can I get all the users? And then how can I filter by Role?
I have the next code:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if (e==null){
Log.i("OK","no problem");
}else{
Log.i("NOT OK","problem");
}
}
});
Honestly, for something as simple as this, it would be better for you not to use relations and separate classes at all and instead to simply add a string field to the User Class called "role." So go to your User class on Parse, add a new column of type String called "role" and then query it as follows.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("role", "admin");
userQuery.findInBackground(new FindCallback<ParseUser>() {
void done(List<ParseUser> results, ParseException e) {
// results has the list of users who are admins
}
});
You should check the ACL(readAccess) of each user. The user is without read access right. The other 9 user may not with public read, not specify the user's id, and the role is not equip access right.
As A.Vin's answer, if your user have more than 1 role, you can use array of strings.

Parse.com android app case insensitive searches

I'm using parse backend for one of my apps. There are three tables and I want to be able to search text entered by the user in search view in the table columns.
private void getDataFromParse() {
ParseQuery<ParseObject> query = ParseQuery.getQuery(Message.NAME);
query.addDescendingOrder(Message.CREATED_AT);
**query.whereStartsWith(Message.MESSAGE, mTag);**
query.setLimit(20);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
mMessages = (List<Message>) (Object) objects;
setAdapter();
}
}
});
}
The problem is that parse search is case sensitive. I enter the table content in parse from their websites UI. One suggestion to solve this problem is here
http://blog.parse.com/learn/engineering/implementing-scalable-search-on-a-nosql-backend/
How do solve my problem ?
You can convert the text enterd by the user to upper case and make sure that all the data in parse are upper case too

Can I update another ParseUser data different by the on logged in?

I want to update a field from table 'User' of a ParseUser different by the one logged in.
The code below is not working and I was wondering if there is some impediment that does not allow to update other's user info.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereMatches("objectId", "4mdsJKKgDJ");
userQuery.getFirstInBackground(new GetCallback<ParseUser>() {
#Override
public void done(ParseUser parseUser, ParseException e) {
// I retreive the parseUser object correctly here
int score = parseUser.getInt("score"); // I have added this column in User table with a default value of 0
parseUser.put("score", score+1);
parseUser.saveInBackground(); // nothing happens. the value is not updated
}
});
Thanks.
I answer myself to my question: you cannot update another user info, different by the one logged in.
Using:
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
System.out.println("ParseException: "+ e);
}
});
revealed the problem:
com.parse.ParseException: java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
So I will need to use another table to keep this info.

Saving data into Parse.com cloud for another user

I have an app which works with users, and offers the possibility to follow certain users, at the current user choice.
Unfortunately, I don't know how to modify data of a user which is not the current user. I have not seen anything like that stated in the Parse.com docs(or i missed).
I have a column in my database in "Users" class called "usersFollowed" and when the current user clicks "Follow" i want to add the current user to the "usersFollowed" list, but adding them like you will see in the following code does not work.
Follow user code:
dialog = ProgressDialog.show(context, "",
"Following...", true);
viewHolder.userFollow.setSelected(true);
viewHolder.userFollow.setText("FOLLOWING");
ParseQuery<ParseUser> userListQuery = ParseUser.getQuery();
userListQuery.whereEqualTo("screenName", parseUserList.get(position).get("screenName").toString());
userListQuery.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> parseObjects, ParseException e) {
parseUserFollowedList = new ArrayList<>();
if (parseObjects.get(0).getList("usersFollowed") == null) {
parseUserFollowedList.add(ParseUser.getCurrentUser());
parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
} else {
parseUserFollowedList = parseObjects.get(0).getList("usersFollowed");
parseUserFollowedList.add(ParseUser.getCurrentUser());
parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
}
parseObjects.get(0).saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
dialog.dismiss();
}
});
}
});
.saveInBackground does not work because it throws exception: Cannot save info for user that is not logged in".
Can anyone help me in how to do this?
Cheers!
Users may modify only their own data. But the idea of following can be implemented such that only the currentUser's record requires write access: If Jack chooses to follow Jill, then Jack writes to his "following" relation. This works as long as we don't try to represent "followedBy" in Jill's data (which can be achieved instead with a query).
Or consider that the User table represents the private relationship between a real person and your app. It might make better sense to model the idea of a user's public face with your own custom object, and model following relationships between those. I mention this idea elsewhere here and here.

Categories

Resources