In my code I convert an anonymous user to a ParseUser, and the data browser shows the converted user with an undefined authData, as it should.
When the app is closed and re-opened, the if statement to check if an anonymous user is linked returns true instead of false, but the authData is still undefined.
We then call a cloud code function to modify the user's friends, and at this point, the current user authData becomes "Anonymous" again, but the username remains the same.
//Returns true no matter what
if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
mUsernameText.setText("Not Logged In");
Log.d("testuser", "tes1");
} else {
mUsernameText.setText(ParseUser.getCurrentUser().getUsername());
}
ParseCloud.callFunctionInBackground
("addFriend", params, new FunctionCallback<String>() {
#Override
public void done(String s, ParseException e) {
if (e == null) {
//ParseUser.getCurrentUser()
// .increment(NUM_FRIENDS_KEY);
//When we try to save the user here it becomes anonymous
}
}
});
Related
I have the following code that allows users to update their data:
String username = "Any username that is currently already used by another user";
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
currentUser.setUsername(username);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// All good!
} else {
// Error: Account already exists for this username.
Log.e("Error: ", e.getMessage());
}
}
}
});
When the user is trying to update the username with one that is already used by someone else, it throws an error:
Account already exists for this username.
Which is exactly what I'd want it to, but when the user goes back (without making another request to change the username to one that is available) the ParseUser.get("username") returns the value that wasn't saved because it already exists (instead of the real value stored at the moment in the server).
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.fetchInBackground(new GetCallback<ParseUser>() {
public void done(ParseUser user, ParseException exception) {
if (exception == null) {
// username now returns ("Any username that is currently already used by another user").
String username = user.get("username").toString().trim();
} else {
// Error
}
}
});
The only way I've found so far to fix the issue is by uninstalling/reinstalling the app. What's causing this? And is there any good way to fix this?
You should backup the current username before setUsername(newName).
if exception happened you have two options either :
Re fetch the user object by ParseUser.getCurrentUser().fetch() (to restore the object to his previous state)
Re set the username to the previous value(that you already back up before)
Why this happened ?
because when you called the setUsername() of the user object you changed your local copy of the object (but you didn't sync it yet with the server) and then when you made save() and the save operation failed the local copy still has the latest changes that you made locally
I am currently trying to setup user login and registration in my app. The first thing I am doing though is having my MainActivity check to see if a user is logged in and if they are, check if the session is valid. I am not sure if I am getting the ParseSession correctly, and if I am, I am not sure what to do at this point. (The docs are not very clear at showing how to accomplish this). Here is my code:
public class MainActivity extends AppCompatActivity {
ParseUser currentUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId(getResources().getString(R.string.parse_app_id))
.clientKey(getResources().getString(R.string.parse_client_key))
.server(getResources().getString(R.string.parse_server_url))
.build()
);
if (isFirstLaunch()) {
Toast.makeText(this, "This is the first launch", Toast.LENGTH_LONG).show();
}
if (!isUserLoggedIn()) {
Toast.makeText(this, "User is not logged in", Toast.LENGTH_LONG).show();
}
}
private boolean isFirstLaunch() {
return false;
}
private boolean isUserLoggedIn() {
currentUser = ParseUser.getCurrentUser();
if (currentUser == null) {
return false;
} else {
ParseSession.getCurrentSessionInBackground(new GetCallback<ParseSession>() {
#Override
public void done(ParseSession object, ParseException e) {
/*
* if session is valid
* return true
* if session is invalid
* run ParseUser.logOut()
* return false
*/
}
});
}
}
}
Session are automatically managed by ParseSession these objects are stored on Parse in the Session class, and you can view them on the Parse.com Data Browser
More information on Session :Sessions represent an instance of a user logged into a device. Sessions are automatically created when users log in or sign up. They are automatically deleted when users log out.
There is one distinct Session object for each user-installation pair; if a user issues a login request from a device they’re already logged into, that user’s previous Session object for that Installation is automatically deleted.
Reference of Site for more detail on Session class.
How to check if logged in user session is valid in Android Parse SDK?
As mentioned above if user is valid (you can check it by ParseUser.getCurrentUser()!= null) and authenticated using isAuthenticated() method of ParseUser class. Session must be valid by default (Automatically managed by ParseSession). And the way you checked for Session is correct but there is no need to check for valid session if user is valid and authenticated.
Hope this is Helpful.
Followed by Parse doc, I created an anonymous user while app is first launched.
...
Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);
ParseUser.enableRevocableSessionInBackground();
if (ParseUser.getCurrentUser() == null) {
ParseUser.enableAutomaticUser();
ParseUser.getCurrentUser().increment("RunCount");
ParseUser.getCurrentUser().saveInBackground();
}
In the parse core, it shows that a new "User" and "Session" row is successfully added.
...
ParseUser.getCurrentUser().setUsername("Leo");
ParseUser.getCurrentUser().setPassword("sasami");
ParseUser.getCurrentUser().signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
...
}
}
...
Then after I calling the "signUpInBackground" method, I found the just-added User row has been updated, but the just-added Session row was disappear. And consequently the user-related query and execution always returns an
com.parse.ParseRequest$ParseRequestException: invalid session token
P.S.
It doesn't matter if signup successfully or returned error. The Session row disappears anyway.
When attempting to login and signup through Facebook, the method isNew() somehow always returns false, even if the "_User" table is empty or if the user is logging in for the first time (signing up).
According to the docs:
isNew(): Indicates whether this ParseUser was created during this session through a call to ParseUser.signUp() or by logging in with a linked service such as Facebook.
Here's the code:
private void loginWithFacebook() {
ParseFacebookUtils.logInWithReadPermissionsInBackground((Activity) getContext(), null, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (user != null) {
if (user.isNew()) {
onLoginListener.onSignupWithFacebook();
} else {
onLoginListener.onLoginWithFacebook();
}
} else if (e != null) {
handleException(e);
}
}
});
}
Other people have come across similar problems and fixed it, but the solutions they've come up with don't apply to my case:
Facebook login with Parse always returns false in user.isNew() Android
Facebook Login not working properly (Parse)
User.isNew() returns false when it should return true
Apart from that, everything else seems to be working fine. The user data is created and stored on the table just like it normally should. Any suggestions?
remove enableAutomaticUser() if you have written in Application or BaseActivity class
docs says that this method will Enables automatic creation of anonymous users.
remove except this three and try may help you.!!
FacebookSdk.sdkInitialize(getApplicationContext());
Parse.initialize(this,getString(R.string.parse_app_id),getString(R.string.parse_client_key));
ParseFacebookUtils.initialize(this);
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
}
}
});