meteor - How can I create a method for retrieving user email address that is available on the client and server? -
i've got facebook, google , regular registration/login turned on on website. problem have email address stored in different fields depending on how user first joined.
for regular users, in field emails[0].address
. facebook , google authenticated users in field services[0].email
.
at various places in client (templates, events) , on server (method), want call 1 method works out field use , returns email address. want similar verification field.
i'm new meteor , ways i've found above far repeat same logic in client , on server doesn't sit me.
the best thing transfer email address 'emails' if log in facebook, google or services first time. make more future proof incase add other services, since meteor use emails.address
(including in other packages)
server side code:
accounts.oncreateuser(function(user) { user.emails = user.emails || []; //if null set empty array if(user.services.facebook) { user.emails.push({address: user.services.facebook.email, verified: false}); }else if(user.services.google) { user.emails.push({address: user.services.google.email, verified: false}); } return user; });
then can check meteor.user().emails[0].address
every time.
note: if not published due autopublish
package may have publish emails
field work on client.
you may have run first time users have logged in before:
meteor.startup(function() { meteor.users({'emails':{$exists: false}}).fetch().foreach(function(user) { var email = (user.services.facebook || user.services.google).email; if(email) meteor.users.update({_id: user._id},{$push:{emails:{address: email, verified: false}}}); }); });
i'm not sure if facebook , google both use email
in services
key, if above should work fine. if don't can edit out key used google or facebook, in case 1 emailaddress
instead of email
example.
Comments
Post a Comment