mikael:accounts-merge

v0.0.11Published 9 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Accounts Merge

Multiple login services for Meteor accounts - enable your users to login to the same account using any login service.

Use case

You decided to allow your users to sign in to your Meteor app with Google, Facebook and Twitter. A user visits your site/app and sign in with Facebook. After enjoying your app for a bit the user leaves and doesn't come back until a week later. The user now can't remeber which login service he/she signed up with and tries to login with Twitter. Now the user has two separate accounts, one for Twitter and one for Facebook. Since the new account is empty the user realize the error and click on the Facebook login. With accounts-merge the two accounts are now merged and in the future the user can sign in to its account using either Facebook or Twitter.

The user can even start two separate accounts, for example one with Google and Twitter and one with only Facebook, and populate both accounts with data. When ever the user is logged in to one of the accounts and decide to sign in to the other account, the accounts are merged and all data from both accounts can be retained/merged.

Example

See this example implementation to get started.

Installation

To enable merging of accounts, add the mikael:accounts-merge package and at least one login provider package: accounts-facebook, accounts-github, accounts-google, accounts-meetup, accounts-twitter, accounts-weibo, etc., ex:

$ meteor add accounts-facebook accounts-google accounts-twitter
$ meteor add mikael:accounts-merge

Upgrade

Before upgrading, check the changelog for breaking changes. Then run:

$ meteor update mikael:accounts-merge

Usage

To use accounts-merge, simply use Meteor.signInWithGoogle() instead of Meteor.loginWithGoogle(). The thing to notice is the callback for signInWithGoogle() is now called with two arguments, error and mergedUserId (the callback for loginWithGoogle() is only called with a single error argument).

1// ON THE CLIENT:
2Meteor.signInWithGoogle ({}, function (error, mergedUserId) {
3
4  // mergedUsers is set if a merge occured
5  if (mergedUserId) {
6    console.log(mergedUserId, 'merged with', Meteor.userId());
7  }
8});
9// Meteor.signInWithFacebook({}, callback);
10// Meteor.signInWithTwitter({}, callback);
1// ON THE SERVER (optional):
2AccountsMerge.onMerge = function (winner, loser) {
3
4  // Update application specific collections, eg.
5  Items.update (
6    {"owner": loser._id},
7    {$set: {"owner": winner._id}},
8    {"multi": true}
9  );
10
11  // If you use something like accounts-guest, you can handle the guest
12  // users here. Eg. when a user with a (one or more) login service(s)
13  // is merged with a guest, then the guest is not a guest anymore!
14  Meteor.users.update (
15    winner._id,
16    {$unset: {"profile.guest": ""}}
17  );
18
19  // Remove the merged (losing) user from the DB
20  Meteor.users.remove(loser._id);
21}

Todo

  • Test with accounts-password (will it work as-is?)
  • Add support for accounts-password
  • Add support for {{loggingIn}}

License

The MIT License (MIT) (c) Airlab.io

Accounts Merge was developed as part of the Domain List project.