Last active
October 4, 2015 13:15
-
-
Save dyaa/d70ccc267e6871a51c3c to your computer and use it in GitHub Desktop.
Connect service to existing meteor account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var addUserService; | |
addUserService = function(service) { | |
if (service === "email") { | |
} else { | |
switch (service) { | |
case "facebook": | |
return Facebook.requestCredential({ | |
requestPermissions: ["email", "user_friends", "manage_notifications"] | |
}, function(token) { | |
return Meteor.call("userAddOauthCredentials", token, Meteor.userId(), service, function(err, resp) { | |
if (err != null) { | |
return Meteor.userError.throwError(err.reason); | |
} | |
}); | |
}); | |
case "google": | |
return Google.requestCredential({ | |
requestPermissions: ["email", "https://www.googleapis.com/auth/calendar"], | |
requestOfflineToken: true | |
}, function(token) { | |
return Meteor.call("userAddOauthCredentials", token, Meteor.userId(), service, function(err, resp) { | |
if (err != null) { | |
return Meteor.userError.throwError(err.reason); | |
} | |
}); | |
}); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="social"><a id="fb" data-service="facebook"><img src="/../facebook.png"></a></div> | |
<div class="social"><a id="go" data-service="google"><img src="/../googleplus.png"></a></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Accounts.onCreateUser(function(options, user) { | |
var email, oldUser, service; | |
/* | |
user.groups = { | |
created: "", | |
invited:"", | |
RSVP:{ | |
coming:"", | |
notComing:"", | |
noReplay:"" | |
} | |
}; | |
*/ | |
if (user.profile == null) { | |
user.profile = options.profile | |
} | |
if (user.services != null) { | |
service = _.keys(user.services)[0]; | |
email = user.services[service].email; | |
if (email != null) { | |
oldUser = Meteor.users.findOne({ | |
"emails.address": email | |
}); | |
if (oldUser != null) { | |
if (oldUser.services == null) { | |
oldUser.services = {}; | |
} | |
if (service === "google" || service === "facebook") { | |
oldUser.services[service] = user.services[service]; | |
Meteor.users.remove(oldUser._id); | |
user = oldUser; | |
} | |
} else { | |
if (service === "google" || service === "facebook") { | |
if (user.services[service].email != null) { | |
user.emails = [ | |
{ | |
address: user.services[service].email, | |
verified: true | |
} | |
]; | |
} else { | |
throw new Meteor.Error(500, "" + service + " account has no email attached"); | |
} | |
user.profile.name = user.services[service].name; | |
} | |
} | |
} | |
} | |
return user; | |
}); | |
userAddOauthCredentials: function(token, userId, service) { | |
var data, oldUser, selector, updateSelector; | |
switch (service) { | |
case "facebook": | |
data = Facebook.retrieveCredential(token).serviceData; | |
break; | |
case "google": | |
data = Google.retrieveCredential(token).serviceData; | |
} | |
selector = "services." + service + ".id"; | |
oldUser = Meteor.users.findOne({ | |
selector: data.id | |
}); | |
if (oldUser != null) { | |
throw new Meteor.Error(500, ("This " + service + " account has already") + "been assigned to another user."); | |
} | |
updateSelector = "services." + service; | |
Meteor.users.update(userId, { | |
$set: { | |
updateSelector: data | |
} | |
}); | |
if (!_.contains(Meteor.user().emails, data.email)) { | |
return Meteor.users.update(userId, { | |
$push: { | |
"emails": { | |
address: data.email, | |
verified: true | |
} | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"click a": function(e) { | |
var service; | |
e.preventDefault(); | |
service = $(event.target).data("service"); | |
return addUserService(service); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment