How to use Accounts.onEmailVerificationLink?
Asked Answered
P

4

6

I'm a bit confused about how to use Accounts.onEmailVerificationLink.
The docs are somewhat ambiguous:

Accounts.onEmailVerificationLink(callback)

Register a function to call when an email verification link is clicked in an email sent by Accounts.sendVerificationEmail. This function should be called in top-level code, not inside Meteor.startup().

What is meant exactly by "this function", the callback, or Accounts.onEmailVerificationLink itself?

Anyway, no matter where I put things, I always get this error message on the browser console:

Accounts.onEmailVerificationLink was called more than once. Only one callback added will be executed.
Penner answered 16/12, 2014 at 9:8 Comment(3)
I'm also getting the Accounts.onEmailVerificationLink was called more than once. Only one callback added will be executed. error even when the Accounts.onEmailVerificationLink() function is empty. Have you figured it out yet?Pigeontoed
I just removed the call and account verification still works, so it must be handled by the useraccounts: package I guess.Penner
No, found it at last: it was in the accounts:ui package (of course...)Penner
M
3

If you use collection hooks (https://atmospherejs.com/matb33/collection-hooks), you can do something like this:

Meteor.users.after.update(function (userId, doc, fieldNames, modifier, options) {
  if (!!modifier.$set) {
    //check if the email was verified
    if (modifier.$set['emails.$.verified'] === true) {
      //do something
    }
  }
});

After spending some time trying to hook into the onMailVerificationLink, I find the above to be much less finicky.

Ministry answered 11/2, 2016 at 2:41 Comment(0)
G
-1
if (Meteor.isClient) {
  //Remove the old callback
  delete Accounts._accountsCallbacks['verify-email'];
  Accounts.onEmailVerificationLink(function (token, done) {
    Accounts.verifyEmail(token, function (error) {
      if (!error) {
        //Do stuff
      }
      done();

    });
  });
}
Giraldo answered 19/12, 2015 at 12:35 Comment(0)
I
-1

The function it is referring to is the 'onEmailVerificationLink' one. It needs to be in some high level client side code. Using the code below I was able to change the functionality for once the email has been verified:

// Override the method that fires when the user clicks the link in the verification email
// for our own behavior.
Accounts.onEmailVerificationLink((token, done) => {
    Accounts.verifyEmail(token, (err) => {
        if (err) {
            console.log("Error: ", err);
        } else {
            console.log("Success");
            // Do whatever you want to on completion, the
            // done() call is the default one.
            done();
        }
    });
});

The console message still appears, but the overridden code runs.

Inapplicable answered 30/10, 2017 at 19:16 Comment(0)
Q
-2

If you can, you should remove accounts:ui package, which is also use it

meteor remove accounts:ui

and then use a callback to add your own logic

Accounts.onEmailVerificationLink(function(token, done) {
  //your own logic
  //Accounts.verifyEmail(token, (error){
  //  if(!error) {
  //    alert('done!');
  //  }
  //});
});
Quixotism answered 10/10, 2015 at 20:2 Comment(1)
I don't know this particular callback, but shouldn't done be called at some point?Sethrida

© 2022 - 2024 — McMap. All rights reserved.