Merging Users in Kinvey

149 views Asked by At

Situation:

  1. User A registers an account in our application and logs in.
  2. For what ever reason logs out.
  3. Logs in to the application again using Facebook social sign on with an account that has the same email associated with it as the original registration had.
  4. A second account is created for this sign on and 2 accounts exist in the system.

How can I merge these accounts into a single account during the social sign on (by querying for the existence of that email) automatically or with User Collection Business Hooks (if with business hooks, can you please provide an example of how I would do this as documentation online is unclear for this specific purpose).

Notes:

  • Kinvey backend
  • Phonegap with facebook plugin
  • Jquery mobile
  • Wish to merge accounts or find existing account and add social identity to it during sign on
  • Assume I cannot delete users
  • Preferably achieve this step with a PreSave Kinvey Business Logic Hook

Cheers,

1

There are 1 answers

0
Aidan On BEST ANSWER

I managed to create my own custom end point to basically achieve user merging.

If a user exists with a kinvey account, then tries to log in using facebook with an email address matching a kinvey user, I used the following end point code to add a social identity to the existing user before attempting to log in using that user (This function only worked because we maintain a guest login for users not logged in to the system).

Hope this helps some one.

function onRequest(request, response, modules) {
  var users = modules.collectionAccess.collection('user');
  var social = request.body.social;
  users.findAndModify({"username":request.body.email},{$set:{"_socialIdentity":social}},
  function(err,result){
    if(err){
      response.error(err);
      response.complete();
    }else{
     if(!result._id){
       response.body = {message:"No User Found Matching This Email"};
       response.complete();
     }else{
       response.body = {message:"User Was Hopefully Updated"};
       response.complete();
     }
    }
  });
}