Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge a user signed up by facebook with an exising user signed up with the same email

I am using parse in an iOS app and there are two ways of signup, either using email/password or Facebook login.

The problem happen when a user sign up using his email/password then logout and try to sign up using his Facebook account which have the same email.

I am using [PFFacebookUtils logInInBackgroundWithReadPermissions:block: to signup with Facebook which creates a new user object in the Users table in Parse

Now I have two records for the same user and I can not update the record that has the Facebook information with the user email because Parse will not allow duplicate emails

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

UPDATE

I have used @kRiZ solution to login using plain Facebook code then either create new user or link the user with the 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 the [PFUser currentUser] to the existing user in case it is already exists

like image 913
Mahmoud Adam Avatar asked Nov 10 '22 04:11

Mahmoud Adam


1 Answers

You can try linking the new Facebook user to an existing user on 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 documentation on linking.

  1. Login via Facebook (do not save user yet)
  2. Query the user table for user having the same email as this new FB user
  3. If found, link, else, save as new user.

UPDATE:

Linking method with FB access token:

[PFFacebookUtils linkUserInBackground:user
                      withAccessToken:accessToken
                                block:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        NSLog(@"Woohoo, the user is linked with Facebook!");
    }
}];
like image 106
kRiZ Avatar answered Nov 14 '22 23:11

kRiZ