How to combine a user registered on facebook with a user registered with the same email address

I use parsing in an iOS application, and there are two ways to register: using email / password or logging in to Facebook.

The problem occurs when a user logs in using their email address / password, and then logs out and tries to register using their Facebook account, which has the same email address.

I use [PFFacebookUtils logInInBackgroundWithReadPermissions:block: to register on Facebook, which creates a new user object in the Users table in Parse

Now I have two entries for the same user, and I can’t update the entry that has Facebook information with the user's email, because Parse will not allow duplicate emails

So what should be the best solution to solve this problem?

UPDATE

I used the @kRiZ solution to log in using a simple Facebook code, either by creating a new user or by linking the user to Facebook data.

 - (void)loginWithFacebookWithSuccessCallback:(APISuccessCallback)successCallback andFailureCallback:(APIFailureCallback)failureCallback { // Login PFUser using Facebook NSArray *permissionsArray = @[@"public_profile", @"email"]; FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; [login logInWithReadPermissions: permissionsArray handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { if (error) { failureCallback(error); } else { FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me?fields=id,first_name,last_name,email,gender" parameters:nil]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id fbResult, NSError *error) { if (!error) { NSString *email = result[@"email"]; User *existingUser = [self getUserByEmail:email]; if (existingUser == nil) { [self signUpNewUserWithEmail:email]; } else { // Need to set the current user to existingUser } [self linkCurrentWithAccessToken:result.token successCallback(@{RESULT:RESULT_OK}); } else { failureCallback(error); } }]; } }]; } 

Now the problem is to assign [PFUser currentUser] to an existing user in case he already exists.

+5
source share
1 answer

You can try associating a new Facebook user with an existing user in Parse using the [PFFacebookUtils linkUserInBackground:*... methods.

 if (![PFFacebookUtils isLinkedWithUser:user]) { [PFFacebookUtils linkUserInBackground:user withReadPermissions:nil block:^(BOOL succeeded, NSError *error) { if (succeeded) { NSLog(@"Woohoo, user is linked with Facebook!"); } }]; } 

See Parse binding documentation .

  • Log in with Facebook (don't save user yet)
  • Request a user table for a user with the same email address as this new FB user
  • If found, link, save as a new user.

UPDATE:

FB Access Token Binding Method:

 [PFFacebookUtils linkUserInBackground:user withAccessToken:accessToken block:^(BOOL succeeded, NSError *error) { if (succeeded) { NSLog(@"Woohoo, the user is linked with Facebook!"); } }]; 
+1
source

Source: https://habr.com/ru/post/1231951/


All Articles