Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse.com PFUser Linking twitter and facebook account?

I am using parse.com as backend of my application, it enables the user to login through his facebook or twitter account. Also it has the feature to link twitter/facebook to his pre-existing account.

Here is the problem:

If a user logins through his facebook account a new PFUser is created on the cloud. If the same user again logins through his twitter account another PFUser is created on the cloud. Now if the user wants to link his facebook account with his twitter account, parse.com replies with "this twitter account is already connected to another user."

How can I merge two PFUsers here? Can anybody please suggest me an approach to solve this issue?

like image 503
Ashwani Avatar asked Apr 26 '13 04:04

Ashwani


3 Answers

Without seeing any code, it sounds like you're calling PFTwitterUtils logInWithBlock when your trying to link a twitter account. That would be the wrong approach since it will just create a new PFUser instead of using the existring PFUser created by signing in with facebook.

What you'll want to do instead is use the PFTwitterUtils method to link the current PFUser with a twitter account. The Parse documentation shows the following code for doing that.

PFUser *user = [PFUser currentUser];
if (![PFTwitterUtils isLinkedWithUser:user]) {
    [PFTwitterUtils linkUser:user block:^(BOOL succeeded, NSError *error) {
        if ([PFTwitterUtils isLinkedWithUser:user]) {
            NSLog(@"Woohoo, user logged in with Twitter!");
        }
    }];
}
like image 90
Craig Siemens Avatar answered Oct 13 '22 17:10

Craig Siemens


You can do one thing which is:

when you link current user with another any Facebook/Twitter account you need to Unlink the the user at some point of time. let's say when you log out you need to check whether current user is linked with Facebook/Twitter ?

if your current user is linked with any of the other user you need to unlink so that you can again link the Facebook/Twitter user with any other user, Like this

For Facebook user :

if([PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]){

                [PFFacebookUtils unlinkUserInBackground:[PFUser currentUser] block:^(BOOL Success,NSError *unlinkError){
                    if(!unlinkError){
                        // User unlinked
                    }else{
                       // Erro while unlink user
                    }
                }];
            }

For Twitter user :

if([PFTwitterUtils isLinkedWithUser:[PFUser currentUser]]){

              [PFTwitterUtils unlinkUserInBackground:[PFUser currentUser] block:^(BOOL Success,NSError *unlinkError){
                    if(!unlinkError){
                        // unlink user
                    }else{
                        // Error while unlink
                    }
}];
}
like image 4
Vinod Singh Avatar answered Oct 13 '22 19:10

Vinod Singh


Regretfully, it seems that it is not possible to have a Facebook or Twitter account linked to more than one PFUser. Additionally, it seems that it s not possible to merge two more more PFUser entries - See Parse's answer here.

What you might want to do instead is keep the data on the PFUser table strictly for authorization purposes and keep the rest of the data in a new and dedicated Parse class (say userData, pointed to by PFUser entries).

In that sense, you'll be able to use multiple PFUser entries pointing to the same userData entry. Thus if you have a previously existing PFUser entry linked to a specific Twitter account in the Parse backend, it's best that you make the user login to that specific PFUser, and then point that entry to your previously existing userData entry.

like image 3
arielyz Avatar answered Oct 13 '22 17:10

arielyz