Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add External Service logins to an already existing account in Meteor?

Tags:

meteor

Having created a profile page for my app, I would like to display a list of social services that the user is on. It struck me that the easiest way would be to use Meteor's built in accounts system for this.

Is there a good way to add external services to an existing account?

Also, will the user then be able to log in with either (e.g.) Facebook and his password from my app?

Another question that naturally follows: Is there a good way to add an application specific password to an account that was created with an external service?

like image 403
Swadq Avatar asked Mar 23 '13 22:03

Swadq


1 Answers

Here's an alternate method. In this solution, I'm overriding a core function and adding some custom behavior. My goal is to associate the service data with the currently logged in user, then allow the core function to do its thing like normal.

orig_updateOrCreateUserFromExternalService = Accounts.updateOrCreateUserFromExternalService;
Accounts.updateOrCreateUserFromExternalService = function(serviceName, serviceData, options) {
  var loggedInUser = Meteor.user();
  if(loggedInUser && typeof(loggedInUser.services[serviceName]) === "undefined") {
    var setAttr = {};
    setAttr["services." + serviceName] = serviceData;
    Meteor.users.update(loggedInUser._id, {$set: setAttr});
  }
  return orig_updateOrCreateUserFromExternalService.apply(this, arguments);
}

Pros:

  • Avoids creation of unnecessary accounts
  • Code is short and easy to understand
  • Code is easy to remove if this functionality is added to Meteor core

Cons:

  • Requires the user to be logged in. If a user logs in with twitter initially, logs out, and then logs in with facebook, then two seperate accounts will be created.
  • Users who share a computer may get their accounts merged unintentionally.
  • Relies on knowledge of how updateOrCreateUserFromExternalService works. This isn't terrible - because it's part of Meteor's public api it probably won't change drastically (not often anyway). But it's still risky.
like image 181
jrullmann Avatar answered Nov 13 '22 10:11

jrullmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!