Had the same issue, plus I also wanted the Google+ login. What I did was to create an Account
object that contains the following field:
- Provider: String - Twitter, Facebook, Google, …
- uniqueId: String - Contains a Twitter ID, Facebook ID, etc.
- User: Pointer - Contains the user associated with this ID
Then I created a script that browse all the users and export their authData
into several Account
entries. I also added an afterSave hook on _User to migrate the new users data.
Now the hard part… Since Parse gives us no hook for a social login, I had to handle everything manually on Cloud Code. The idea is to use the SDK from Facebook and Twitter to get the user's account ID and access token.
Then you send those data to a loginFromFacebook
/LoginFromTwitter
cloud function, and control that the Facebook/Twitter ID matches the access token (by doing a request to the Facebook's Graph API for example). Once you know the user id you have is correct, you need to check if it exists in Account
. If yes you return (with response.success) the session token of the Parse user associated to this id (the Account.user field) and use User.become
on the client to finish login the user. If not, you need to create a new user, add en entry in Account
, and return the new user's session token (+ doing the User.become on the client).
Now, thanks to the Account
table, you can easily link/unlink users by simply changing the associated user ID in the Account
table. You can also easily check for Facebook friends (if you request the permission).