Start a new topic

Merge Users and/or PreSave hook code for merging

Hi,


If a user creates an account on our app (kinvey account), then logs out and tries to sign in with a facebook account that has the same email it creates a new user.


Can a Kinvey Engineer please show me example PreSave hook code for achieving the following:


1. User creates normal account.
2. For what ever reason logs out.
3. User signs in with facebook that has an email matching the initial created account.

4. Instead of creating a new user I would like this facebook sign in to merge with the existing account and log the user in as normal.


An example of how to do this functionality with PreSave hooks would be very helpful as the online documentation doesn't really cover this situation.


Currently using:
- Kinvey

- Phonegap
- Facebook Plugin for Phonegap



Cheers,


Any one out there with any helpful information?

 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();
     }
    }
  });
}

 

Login or Signup to post a comment