Hi I read the tutorials on basic parse features, etc. and I am using the ParseUser feature. From the tutorial this is shown :
ParseUser.logInInBackground("Jerry", "showmethemoney", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.
} else {
// Signup failed. Look at the ParseException to see what happened.
}
}
});
I was wondering how I could look at the ParseException to see what happened. For example, I want to inform the user that a bad username was entered, or a bad password.
Thank you
a Pretty easy way to resolve this is to use
e.getMessage();
// this probably shows you appropriate message
// or you can use
e.getCode();
// and match code with the integers given in Parse Doucmentation
// for example.
// e.EMAIL_NOT_FOUND is code for when email is not found in registered users class
if you need see the exception, can try show in the log or where you decide, in your code for te show in the log where Log.d(nameofalert,mensaje), only need see the logcat for the identify your exception
ParseUser.logInInBackground("Jerry", "showmethemoney", new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
Log.d("user not null",e.toString());
} else {
Log.d("error",e.toString());
// Signup failed. Look at the ParseException to see what happened.
}
}
});
Place your code in between try catch and in the catch block try to handle stuff that will go when user enter bad username or password
try{
}
catch(ParseException e)
{
// do stuff here
}
how about just doing e.printStackTrace(), that will print all the information that you need pertaining to the exception
Related
I'm using Parse as my backend and I'm trying to "like" a post that another user posted on the app. I'm querying to get the post, then incrementing the number of likes by 1, then adding the current user's object ID to an array that holds all the ID's of users which liked the post.
carLikeQuery.getInBackground(carItem.getObjectId(), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
object.increment("likes");
object.addUnique("usersWhoLike", ParseUser.getCurrentUser().getObjectId());
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if(e==null) {
Log.d("SAVE", "Like saved :)");
} else {
Log.e("SAVE", "Not saved :( :" + e.getLocalizedMessage());
}
}
});
}
});
The error I'm getting:
E/SAVE: Not saved :( :java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
I saw the source code for the ParseUser from somewhere:
void validateSave() {
if (getObjectId() == null) {
throw new IllegalArgumentException("Cannot save a ParseUser until it has been signed up. Call signUp first.");
}
if (!isAuthenticated() && isDirty()) {
throw new IllegalArgumentException("Cannot save a ParseUser that is not authenticated.");
}
}
Doing the same kind of checking in my code reveals that the the currentUser is AUTHENTICATED and NOT DIRTY.
What could the issue be? To be honest, I want to say that it was working just fine before today, but obviously I was changing something and made a mistake down the line and I can't find it! Any help would be greatly appreciated.
I resolved the issue by creating an entirely new Parse application with the same data structure/layout. It just plain worked without any code changes.
You can follow the issue on GitHub here
I have a user signup method that looks like this:
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Link user to the 'User' role
ParseQuery<ParseRole> roleQuery = ParseRole.getQuery();
roleQuery.whereEqualTo("name", "User");
roleQuery.getFirstInBackground(new GetCallback<ParseRole>() {
#Override
public void done(ParseRole parseRole, ParseException e) {
if (e == null) {
//final ParseRole tempParseRl = parseRole;
ParseRelation<ParseUser> tempRel = parseRole.getUsers();
Log.i("ParseRole: ", parseRole.getName().toString());
parseRole.getUsers().add(user);
//TODO: 4. Delete reg key used for this user
}
// Error on Role ACL
else {
dMenuVerData.dismiss();
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
//1. Hide the progress bar
dataVerPBar.setVisibility(View.GONE);
//2. Show okBtn & successTxt
okBtn.setVisibility(View.VISIBLE);
userSuccessTxt.setVisibility(View.VISIBLE);
dataVerTitle.setText("Congratulations!");
//ParseRelation<ParseUser> tempRel = new ParseRelation<ParseUser>();
//tempParseRl.put("users");
} else {
// Dismiss dialog, show Parse error
dMenuVerData.dismiss();
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
On Parse I have two roles created on the data-browser.
After executing the line:
<role>.getUsers().add(user);
I'm expecting to be able to see the recently signed-up user under the "users , instead this table is empty:
What am I missing? Is the
<role>.getUsers().add(user);
working properly? Thanks.
The solution resides in using <role>.saveInBackground(); method after adding a new user to the ParseRelation, so after the signup succeeded I added:
...
parseRole.getUsers().add(user);
parseRole.saveInBackground();
...
I hope this helps.
you are not saving the role after you changed the users relation
what can i see that you added the user to (the local list you get from parseRole.getUsers() but you didn't saved it (to sync the changes with the remote version)
I'm currently building an app that has Parse at its backend. I've realized that a user can log in to his account in multiple devices. Is there a way I could restrict this to only one device at one time? If so can you kindly explain the method.
Thanks in advance.
you need to monitor the application will your web environment (webservice for example) As soon as someone you login you must disconnect the other devices connected to the same user.
You can analyze it by IMEI who made the last login request and send a command to the other devices of the same user to remove access.
I came here looking for a solution and didn't find one. Through trial and error, I have solved it. I am a novice developer and do not know if this is the best practice, but it works. When the user attempts to log in execute the following.
public void login(View v) {
// Create string variables for data.
String username = et_username.getText().toString().toLowerCase().trim();
String password = et_password.getText().toString();
// If a user is already on this device, log them out.
// this will happen when the force log out happens, the device will
// simply catch the invalid session, but still have that user data, just unavailable
user = ParseUser.getCurrentUser();
if (user != null)
user.logOutInBackground();
ParseUser.logInInBackground(username, password, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
final ParseQuery<ParseObject> query = new ParseQuery<>("_Session");
query.include("user");
query.whereEqualTo("user", ParseUser.getCurrentUser()); // Only get sessions for the specific user.
query.addAscendingOrder("createdAt"); // Place them in chronological order, with the oldest at the top.
query.countInBackground(new CountCallback() {
#Override
public void done(int count, ParseException e) {
if (count > 1) {
try {
query.getFirst().deleteInBackground();
} catch (ParseException e1) {
Toast.makeText(LoginActivity.this, e1.toString(), Toast.LENGTH_LONG).show();
}
}
}
});
if (user != null) {
emailVerified = user.getBoolean("emailVerified");
if (emailVerified) {
// Update the database to track their log in.
user.put("loggedIn", true);
user.saveInBackground();
if (!deliquent) {
// Launch the MainActivity.
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
else {
Intent intent = new Intent(LoginActivity.this, CardActivity.class);
startActivity(intent);
}
}
else {
Toast.makeText(LoginActivity.this, getResources().getString(R.string.verify), Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(LoginActivity.this, "From LoginActivity line:213\n" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
I am developing android application and using parse.com as back end storage. But I got stuck on change password. I am able to send the reset password mail using parse.com sdk to particular email. but I want to change the password using application as well without log enter code herein using old password.
Function to send mail:-
public void resetPassword() {
CustomProgressDialog.show(LoginActivity.this, "", getResources()
.getString(R.string.please_wait));
ParseUser.requestPasswordResetInBackground("test#gmail.com",
new RequestPasswordResetCallback() {
public void done(ParseException e) {
CustomProgressDialog.dismissMe();
if (e == null) {
// An email was successfully sent with reset
// instructions.
Toast.makeText(getApplicationContext(), getResources().getString(R.string.reset_password_sent), Toast.LENGTH_LONG).show();
} else {
// Something went wrong. Look at the ParseException
// to see what's up.
Toast.makeText(getApplicationContext(), getResources().getString(R.string.reset_password_fail), Toast.LENGTH_LONG).show();
}
}
}
);
}
And also able to launch the application from mail using declaring the permission in AndroidManifest.xml.
You can use for that next method ParseUser.setPassword().
Idea is next, if user is logged in then you don't need to check old password, because it was already entered and applied by Parse.com. So you will have 2 fields New Password and Confirm New Password. Users enters them and application changes it on server.
ParseUser parseUser = ParseUser.getCurrentUser();
parseUser.setPassword(password);
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (null == e) {
// report about success
} else {
// report about error
}
}
});
I'm trying to set the email in a user obtained from Facebook SDK. If I set the email with any string like this:
user.setEmail("whatever#whwhwh.com");
user.saveInBackground();
It works, no problem... it appears in Parse's Data Browser...
If I try to do what I need to do(see code below), then it doesn't work, I've checked the String value and it's ok, it is the needed email....I've tried different things.. no luck.. Please help, thanks.
user.setEmail(user.getProperty("email").toString());
user.saveInBackground();
Make sure the email being supplied is not used by any other user. This is because an email must be unique.
When calling saveInBackground, it will actually not throw any exception if one does occur. So in your case, if the email is a duplicate, the email won't get saved and no exception is thrown.
To catch an exception with saveInBackground, you can use the SaveCallback.
user.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
// No exception
} else {
// Exception occured
}
}
});
For a list of exceptions you can refer to ParseException.