Reputation: 1538
In my meteor-angular application, i have 2 accounts-login configured: Facebook and Google
Now, lets say that user A logged in with his Facebook account. After the login, i'm sending the user to an additional form page to insert his social id(this form page is been shown to the user only once).
So now i have a user object(Meteor.user()
+ his social id).
The user is working in the application and insert his assets.
Now the the user decides to logout.
After a while he goes back to the application and this time he logs in with his Google account. So now he will be redirected to the "Insert Social ID" page form again since a new user was created in the DB.
Is there a way to bind those 2 users? I have a unique identifier(social id) which could help me merge them but do not find a way to do that binding...
I need that binding because if he is logged with different id, he won't be able to edit his assets...
10x
Upvotes: 3
Views: 804
Reputation: 6564
Use the onCreateUser
callback. Find an already registered user with that social ID, and if it exist, then just add the new service object to the existing user document.
Accounts.onCreateUser(function (options, user) {
// Find an existing user based on social ID
let registeredUser = Meteor.users.findOne(...);
if(registeredUser) {
let googleProfile = user.services.google;
if (googleProfile) {
// Add Google login credentials to the user document in Mongo
Meteor.users.update({...}, {$set: {"services.google": googleProfile});
}
// throw an Error here to stop creating a new user.
}
// Returning a user object will create a new one in Mongo.
return user;
});
Disclaimer: I would generally advise against merging users. Please consider that a malicious attacker who stole your Facebook password could steal your data in this service too, even if you only registered using Google.
Upvotes: 3
Reputation: 1
There is a package called splendido:accounts-meld that merge all the users social informations coming from the same email and also merge the user with that verified-email.
You can find more details on the github repository: https://github.com/splendido/meteor-accounts-meld
Upvotes: 0