splendido / meteor-accounts-emails-field

A Meteor package to keep user.email up to date with email addresses used for 3rd-party account services
19 stars 9 forks source link

Any way to be informed when registered_emails changes? #40

Closed steph643 closed 9 years ago

steph643 commented 9 years ago

I keep, in Meteor.users, an officialContactEmail field which I need to update whenever registered_emails changes. How can I do that?

splendido commented 9 years ago

I'd say with an observeChanges

steph643 commented 9 years ago

I would prefer to avoid a client-side observer on (hopefully) the biggest collection of my application :-) I will rather try to add my own Accounts.onLogin hook. Do you know how to be sure my hook gets called after yours?

splendido commented 9 years ago

...it might also be that you don't publish the whole users collection to your client ;-)

Kidding apart, package code is loaded before the top level code of your app, so any onLogin hook you set up should come after the ones set by packages.

steph643 commented 9 years ago

It works, thanks.

To people who might need this in the future, notice that there is a minor inconvenience when adding your on onLogin hook: when a user logs-in for the first time, the info.user argument does not contain the registered_emails field. So you will need to load it like this:

    Accounts.onLogin(function (info) {
        // Get the user (if login was successful)
        var user = info.user;
        if (! user)
            return;

        // Get the registered_emails that the meteor-accounts-emails-field package has just set
        user = Meteor.users.findOne(user._id, { fields: { registered_emails: 1, ... } });
        check(user.registered_emails, [Object]);

        ...
    });
splendido commented 9 years ago

oh, right!

this is actually needed all the time, since modification to the info object are not propagated through different onLogin callbacks!

I also had to do the same for accounts-meld: see these lines

splendido commented 9 years ago

Could you prepare a simple PR to add something about this into the README?

steph643 commented 9 years ago

Ok, I will have a look.

splendido commented 9 years ago

Done, tnx!