There seems to be lots of people with issues using parse.com's ParseFacebookUtils.logIn() method and I am experiencing a very strange issue myself (none of my issues are similar to those I have seen here on SO).
I have been developing an application for quite some time now and everything has been working perfectly. It wasn't until I logged out of UserA's Facebook account using the app on my test/development phone in order to log into UserB's account testing purposes that problems started to occur. Now every time any user logs into my app using the Sign In With Facebook
button a new user is created in my app's User
class in the parse database. Very strange. The code that I have for Facebook login is identical to the example app given by parse.com itself to demonstrate how to do it - nothing custom. Here it is:
private void onLoginButtonClicked() {
LoginActivity.this.progressDialog = ProgressDialog.show(
LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "user_friends",
"user_relationships", "user_birthday", "user_location");
ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException err) {
LoginActivity.this.progressDialog.dismiss();
if (user == null) {
Log.d(TAG,
"Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d(TAG,
"User signed up and logged in through Facebook!");
/*this will populate common info from the user's Facebook profile and store it in Parse backend */
facebookObject.getMyFacebookDetails();
showNewsFeedActivity();
} else {
Log.d(TAG,
"User logged in through Facebook!");
//facebookObject.getMyFacebookDetails();
ParseUser currentUser = ParseUser.getCurrentUser();
Logging.toast("Welcome back, " + currentUser.getString("firstName")+"("+currentUser.getObjectId()+")");
showNewsFeedActivity();
}
}
});
}
I also have the following in my onActivityResult method as it is in the example:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CREATE_ACCOUNT_CODE){
showNewsFeedActivity();
}else if (requestCode == SIGN_IN_WITH_EMAIL_CODE){
showNewsFeedActivity();
}else if (requestCode == FORGOT_PASSWORD_CODE){
...
}
else{
ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
}
}
Here is the link to the parse.com example app https://parse.com/tutorials/integrating-facebook-in-android that uses the exact method.
Ever since I logged out of UserA's Facebook account and then logged in with UserB's credentials and then logged into my app using the Sign In With Facebook button it now creates a new user that I can see in my app's dashboard on parse.com in the User class. Even when I log back into UserA and try to log in a new user will be created even though there is already a user (myself) with an account.
Questions I have:
1) Is there something I should be doing differently to save a new user? As of right now I don't have any different logic than the ParseFacebookUtils.logIn() because that call says that it will automatically create a new user if one doesn't already exist. I would assume that the same call to that method would also be able to somehow tell that a user with that Facebook Id already exists but maybe this is what I am not doing or misunderstanding (of course this that is handled internally by ParseFacebookUtils so I shouldn't have to touch any of that).
If you have any ideas on what is happening I would greatly appreciate to hear them.
EDIT:
The user.isNew() method is hit on each login. The call to this function will be true when the user is created as a result of this call to FacebookUserUtils.logIn(). So obviously something is wrong since this user is actually not new (they are already in my app's database but for some reason something is being ignored).
It turns out the reason this was happening was because I was making a call to ParseFacebookUtils.unlink(user) upon logout which was clearing the Facebook authentication association with this user which in turn would force the call to .login() to create a new user in the database. So only use unlink when you actually have a need to remove the Facebook auth (in my case it will be if the user decides to remove their account).